0
有一个MVC应用程序和一个单独的WebAPI。使用plupload时,当url指向MVC控制器中的一个方法时,这些文件将被POST。plupload提交OPTIONS而不是POST到WebAPI
这里是提琴手显示
POST /Home/HandleUpload/ HTTP/1.1
Host: localhost:50000
Connection: keep-alive
Content-Length: 38040
Origin: http://localhost:50000
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryT4glpqFi5sbmY2KL
Accept: */*
Referer: http://localhost:50000/Home/Index
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
当我更改URL指向的WebAPI,我得到OPTIONS请求而不是POST,因此API方法不被打到。
OPTIONS /api/v1/Files/HandleUpload HTTP/1.1
Host: localhost:60000
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:50000
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://localhost:50000/Home/Index
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
我在plupload配置上改变的唯一的事情就是url。
这是我的方法。两个项目都是一样的。
[HttpPost]
public HttpResponseMessage HandleUpload(int? chunk, string name)
{
var fileUpload = HttpContext.Current.Request.Files[0];
var uploadPath = HttpContext.Current.Server.MapPath("~/App_Data");
chunk = chunk ?? 0;
//write chunk to disk.
string uploadedFilePath = Path.Combine(uploadPath, name);
using (var fs = new FileStream(uploadedFilePath, chunk == 0 ? FileMode.Create : FileMode.Append))
{
var buffer = new byte[fileUpload.InputStream.Length];
fileUpload.InputStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
}
你似乎在做一个跨域请求。查看本教程获取更多信息:http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api –
我可以将[HttpPost]更改为[HttpOptions] API和我会打的方法。在这种情况下,Request.Files [0]为空,因此引发异常。我期望MVC的请求是一个POST而不是OPTIONS。 – gene
我的表单enctype是multipart/form-data。这个问题可能是因为我的表单行为与我的plupload url不同吗? – gene