我正在关注 Flask 的 Google Blobstore api 文档(按预期工作)并尝试获取上传的文件信息:
class PhotoUploadHandler(blobstore.BlobstoreUploadHandler):
def post(self):
upload = self.get_uploads(request.environ)[0]
photo = PhotoUpload(blob_key=upload.key())
photo.put()
logging.info(f"upload: {upload}")
但是,在我已经实现的处理程序上,该文件在 request.environ 上下文中不可用。这是我的经纪人:
@upload_testing_bp.route("/")
def upload():
upload_url = blobstore.create_upload_url("/upload_testing/upload_photo", gs_bucket_name="test")
response = make_response(jsonify(upload_url))
response.headers["Cache-Control"] = "no-store"
response.headers["Content-Type"] = "application/json"
return response
@upload_testing_bp.route("/upload_photo", methods=["POST"])
def upload_photo():
upload = self.get_uploads(request.environ)[0]
这是上传按钮:
<div class="button hidden-upload" type="button" ng-show="dr.folder.folder.rights[1]" ng-click="!filereader_available && no_filereader()">
<span class="material-icons btn-icon file_upload"></span>
<span class="btn-icon-label">Upload</span>
<input type="file" name="files[]" multiple="" dr-upload="" ng-if="filereader_available">
</div>
以及对生成的 url 的 POST 请求:
function upload(file, backoff) {
var fd = new FormData();
fd.append("folder", folder_key_str);
fd.append("file_name", file_item.name);
fd.append("files[]", file);
$http.post(url, fd, {
headers: {'Content-Type': undefined},
transformRequest: angular.identity,
timeout: 1000 * (min_timeout + Math.pow(1.5, backoff))
})
知道我的实施可能有什么问题吗? POST 请求似乎符合预期,文件在 request.environ 上不可用,但在
request.files['files[]']
上可用
经过进一步调试,该文件实际上在 request.environ 上可用,但是在同一个 POST 请求中,我还尝试发送其他参数,并且一旦我获得这些参数的值,例如:
request.form.get('file_name')
该文件不是在当前 request.environ 上不再可用,如果我首先获取文件信息,同样会发生相反的情况: blob_info = self.get_uploads(request.environ)[0]
其他表单参数不再可用。
我找到的解决方案是将其他请求参数作为查询参数发送,然后将它们获取为:file_name = request.args.get('file_name')