Webapi ajax formdata upload with extra parameters

后端 未结 1 1970
故里飘歌
故里飘歌 2020-12-06 07:27

I\'m using jQuery ajax to upload file but want to add some parameters on webapi method, here is:

var data = new FormData();
data.append(\"file\", $(\"#file\"         


        
相关标签:
1条回答
  • 2020-12-06 07:59

    Posting the FormData object results in a request with content type multipart/form-data. You have to read the request content like so:

    [HttpPost]
    public async Task<string> Upload()
    {
        var provider = new MultipartFormDataStreamProvider("C:\\Somefolder");
        await Request.Content.ReadAsMultipartAsync(provider);
    
        var myParameter = provider.FormData.GetValues("myParameter").FirstOrDefault();
        var count = provider.FileData.Count;
    
        return count + " / " + myParameter;
    }
    

    BTW, this will save the file in the path specified, which is C:\\SomeFolder and you can get the local file name using provider.FileData[0].LocalFileName;

    Please take a look at MSDN code sample and Henrik's blog entry.

    0 讨论(0)
提交回复
热议问题