【发布时间】:2020-06-09 11:38:09
【问题描述】:
我正在使用 nginx 提供一个页面,该页面包含一个用于将文件上传到服务器的表单。我在 POST 请求中使用 FormData() 类作为我的数据:
js:
$.ajax({
url: /upload,
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
enctype: 'multipart/form-data',
processData: false,
success: function (response) {
alert(response);
}
});
我正在使用示例 nginx 上传模块,使用本页底部的默认配置:https://www.nginx.com/resources/wiki/modules/upload/:
location /upload {
# Pass altered request body to this location
upload_pass @test;
# Store files to this directory
# The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
upload_store /tmp 1;
# Allow uploaded files to be read only by user
upload_store_access user:r;
# Set specified fields in request body
upload_set_form_field $upload_field_name.name "$upload_file_name";
upload_set_form_field $upload_field_name.content_type "$upload_content_type";
upload_set_form_field $upload_field_name.path "$upload_tmp_path";
# Inform backend about hash and size of a file
upload_aggregate_form_field "$upload_field_name.md5" "$upload_file_md5";
upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size";
upload_pass_form_field "^submit$|^description$";
upload_cleanup 400 404 499 500-505;
}
# Pass altered request body to a backend
location @test {
proxy_pass http://localhost:8080;
}
我没有使用任何 PHP 脚本或其他任何东西;我的印象是默认的上传模块配置只会将上传的文件转储到 /tmp/1(我已经创建了这个目录并授予了 nginx 的写权限)。该文件只有几 kb,因此文件大小应该没有问题。
当我尝试上传文件时,我收到 HTTP 415 错误,即不支持的媒体类型。我上传的文件有一个扩展名 .bin,这是 /etc/nginx/mime.types 文件中支持的 MIME 类型。
我可以使用 cURL 在命令行上发出类似的请求:
curl -F 'file@=myfile.bin' http://<URL>/upload
这很好用。检查 curl 的输出,我在 POST 标头中看到了
Content-Type: multipart/form-data; boundary=-------etc
但这在上面的 jQuery ajax 调用中被禁用为
Content-Type: false;
如How can I upload files asynchronously? 等 SO 帖子中所建议的那样。 如果我在对 multipart/form-data 的 ajax 调用中强制使用 Content-Type,则会收到 400 Bad Request 错误。 谁能解释我哪里出错了?
【问题讨论】:
标签: javascript jquery ajax nginx