手動アップロード

□未翻訳

□翻訳中

■翻訳完了(細田謙二)

■レビュー(Omi Chiba)

手動アップロード

次のモデルを考えてください:

Consider the following model:

1.

db.define_table('myfile',Field('image','upload'))

通常、挿入は、SQLFORMやcrudフォーム(SQLFORMの1つ)を介して自動的に処理されます。しかし、場合によっては、ファイルシステム上にすでにファイルがあり、プログラムでアップロードしたいことがあります。これは、次のような方法で行うことができます:

Normally an insert is handled automatically via a SQLFORM or a crud form (which is a SQLFORM) but occasionally you already have the file on the filesystem and want to upload it programmatically. This can be done in this way:

1.

2.

stream = open(filename,'rb')

db.myfile.insert(image=db.myfile.image.store(stream,filename))

uploadフィールドオブジェクトのstoreメソッドは、ファイルストリームとファイル名を受け取ります。ファ イル名はファイルの拡張子(型)を決めるのに使用され、(web2pyのアップロード機構に従って)そのファイルのための新しい仮の名前が作成さ れ、(特に指定がなければuploadsフォルダの下の)その新しい仮のファイルにファイルの内容がロードされます。そして、新しい仮のファイル名が返され、db.myfileテーブルのimageフィールドに保存されます。

The store method of the upload field object takes a file stream and a filename. It uses the filename to determine the extension (type) of the file, creates a new temp name for the file (according to web2py upload mechanism) and loads the file content in this new temp file (under the uploads folder unless specified otherwise). It returns the new temp name, which is then stored in the image field of the db.myfile table.