MVC アプリケーションと別の WebAPI を用意します。url が MVC コントローラーのメソッドを指している場合、plupload を使用すると、ファイルが POST されます。
これがフィドラーが示すものです
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
WebAPI を指すように URL を変更すると、POST ではなく OPTIONS リクエストが返されるため、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);
}
}