相关疑难解决方法(0)

ASP.NET Web API文件另存为"BodyPart_3ded2bfb-40be-4183-b789-9301f93e90af"

我正在使用ASP.NET Web API上传文件.我在RC之前做过这个,但由于某种原因,文件被保存为"BodyPart_3ded2bfb-40be-4183-b789-9301f93e90af"而不是文件名.下面的filename变量也会返回此bodypart字符串而不是文件名.我似乎无法弄清楚我哪里出错了.任何帮助表示赞赏.

客户代码:

function upload() {
    $("#divResult").html("Uploading...");
    var formData = new FormData($('form')[0]); 
    $.ajax({
        url: 'api/files/uploadfile?folder=' + $('#ddlFolders').val(),
        type: 'POST',
        success: function (data) {
            $("#divResult").html(data);
        },
        data: formData,
        cache: false,
        contentType: false,
        processData: false
    });
}; 
Run Code Online (Sandbox Code Playgroud)

控制器:

    public Task<HttpResponseMessage> UploadFile([FromUri]string folder)
    {
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
        }

        // Save file
        MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(HttpContext.Current.Server.MapPath("~/Files"));
        Task<IEnumerable<HttpContent>> task = Request.Content.ReadAsMultipartAsync(provider);

        return task.ContinueWith<HttpResponseMessage>(contents =>
        {
            string filename = provider.BodyPartFileNames.First().Value;
            return new HttpResponseMessage()
          {
              Content = new StringContent(string.Format("File saved in {0}.", …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc http multipartform-data file asp.net-web-api

34
推荐指数
3
解决办法
2万
查看次数