2013-10-31 36 views
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); 
    } 
} 
+3

你似乎在做一個跨域請求。查看本教程獲取更多信息:http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api –

+0

我可以將[HttpPost]更改爲[HttpOptions] API和我會打的方法。在這種情況下,Request.Files [0]爲空,因此引發異常。我期望MVC的請求是一個POST而不是OPTIONS。 – gene

+0

我的表單enctype是multipart/form-data。這個問題可能是因爲我的表單行爲與我的plupload url不同嗎? – gene

回答

0

我能夠做到這一點,但不知道這是否是最佳做法。文件名設置在UI中preinit功能

preinit: { 
          UploadFile: function (up, file) { 


           // You can override settings before the file is uploaded 
           // up.settings.url = 'upload.php?id=' + file.id; 
           //up.settings.multipart_params = { type: $("#Type").val(), title: $("#Title").val() }; 
           up.settings.multipart_params = { 
            filename: file.name 
           }; 

          } 
         }, 

的Web API代碼

[HttpPost] 
      public async Task<IHttpActionResult> UploadPropertyImage() 
      { 
       if (!Request.Content.IsMimeMultipartContent()) 
        throw new Exception(); // divided by zero 

       var provider = new MultipartMemoryStreamProvider(); 
       await Request.Content.ReadAsMultipartAsync(provider); 



       var name = await provider.Contents.SingleOrDefault(p => p.Headers.ContentDisposition.Name == "\"name\"").ReadAsStringAsync(); 
       var chunk = await provider.Contents.SingleOrDefault(p => p.Headers.ContentDisposition.Name == "\"chunk\"").ReadAsStringAsync(); 
       var chunks = await provider.Contents.First(p => p.Headers.ContentDisposition.Name == "\"chunks\"").ReadAsStringAsync(); 
       var filename = await provider.Contents.First(p => p.Headers.ContentDisposition.Name == "\"filename\"").ReadAsStringAsync(); 
       var buffer = await provider.Contents.First(p => p.Headers.ContentDisposition.Name == "\"file\"").ReadAsByteArrayAsync(); 
       //var Id = await provider.Contents.First(p => p.Headers.ContentDisposition.Name == "\"Id\"").ReadAsByteArrayAsync(); 
       var Id = Guid.Empty; 

       var uploadPath = HostingEnvironment.MapPath(Path.Combine("~/app_data",Id.ToString())); 


       if (!Directory.Exists(uploadPath)) 
        Directory.CreateDirectory(uploadPath); 

       using (var fs = new FileStream(Path.Combine(uploadPath,name), chunk == "0" ? FileMode.Create : FileMode.Append)) 
         fs.Write(buffer, 0, buffer.Length); 


       return Ok(); 
      } 
相關問題