Javascript 如何使用 JQuery 使用 HTTP“PUT”上传文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5518420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:35:18  来源:igfitidea点击:

How to upload a file using an HTTP "PUT" using JQuery?

javascriptjqueryput

提问by Sled

I would like to upload a file using JQuery-File-Upload, but using HTTP "PUT" instead of multipart-forms. According to their site:

我想使用 JQuery-File-Upload 上传文件,但使用 HTTP“PUT”而不是 multipart-forms。根据他们的网站

- Multipart and file contents stream uploads:
    Files can be uploaded as standard "multipart/form-data" or file contents stream (HTTP PUT file upload).

but I cannot find anywhere in their documentation as to how to do this. Can anyone help?

但我在他们的文档中找不到任何关于如何做到这一点的地方。任何人都可以帮忙吗?

采纳答案by mathieu

According to : https://github.com/blueimp/jQuery-File-Upload/wiki/Options

根据:https: //github.com/blueimp/jQuery-File-Upload/wiki/Options

method

The method of the HTTP request used to send the file(s) to the server. Can be POST (multipart/formdata file upload) or PUT (streaming file upload). Accepts a String or a function returning a String.

方法

用于将文件发送到服务器的 HTTP 请求的方法。可以是 POST(multipart/formdata 文件上传)或 PUT(流文件上传)。接受字符串或返回字符串的函数。

You should use :

你应该使用:

$('#file_upload').fileUpload({
    namespace: 'file_upload_1',
    url: '/path/to/upload/handler.json',
    method: 'PUT'
});

回答by daniellmb

I love REST too but you might want to make sure you unit test well on the browsers you need to support.

我也喜欢 REST,但您可能希望确保在您需要支持的浏览器上进行良好的单元测试。

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.ajax/

The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they may not supported by older browsers.

要发出的请求类型(“POST”或“GET”),默认为“GET”。注意:此处也可以使用其他 HTTP 请求方法,例如 PUT 和 DELETE,但较旧的浏览器可能不支持它们。

See this answer How do I PUT data to Rails using JQuery

请参阅此答案 How do I PUT data to Rails using JQuery