LoginSignup
0
0

More than 5 years have passed since last update.

medium-editor-insert-pluginに画像ファイルの容量制限を設定する

Posted at

Railsにmedium-editorでMedium.comライクな投稿機能を導入するで導入まですませている前提になります。

やりたいこと

画像を登録する際に、容量制限を設定したい。

調査

画像を保存する部分には、medium-editor-insert-pluginを使っている。
ドキュメントを読んで見ると、jQuery-File-Uploadをラップしてアップロード処理を行なっていた。

medium-editor-insert-pluginのconfigrationに以下のようにあった。

image.png

jQuery File Uploadを見に行く

maxFileSize
The maximum allowed file size in bytes.

Type: integer
Default: undefined
Example: 10000000 // 10 MB
Note: This option has only an effect for browsers supporting the File API.

maxFileSizeに関する記述を見つけた。

maxFileSizeを設定する

$('.editable').mediumInsert({
  editor: editor,
  addons: {
    images: {
      useDragAndDrop: false,
      fileUploadOptions: {
        url: '/images/upload',
        type: 'post',
        // 以下を追加
        maxFileSize: 3000000, //3MBで制限を掛ける
        acceptFileTypes: /(.|\/)(gif|jpe?g|png)$/i
      },
      fileDeleteOptions: {
        url: '/images/delete',
      },
      messages: {
        acceptFileTypesError: 'This file is not in a supported format: ',
        maxFileSizeError: 'This file is too big: '
      }
    }
  }
});

大きな画像をアップロードしてみます。

image.png

大きな画像を防ぐことが出来ました。
以上です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0