的ASP.NET Web API文件保存为" BodyPart_3ded2bfb-40BE-4183-b789-9301f93e90af" [英] ASP.NET Web API File saved as "BodyPart_3ded2bfb-40be-4183-b789-9301f93e90af"

查看:155
本文介绍了的ASP.NET Web API文件保存为" BodyPart_3ded2bfb-40BE-4183-b789-9301f93e90af"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上传使用ASP.NET Web API的文件。我在之前RC已经做了这一点,但由于某些原因,文件被保存为BodyPart_3ded2bfb-40BE-4183-b789-9301f93e90af,而不是文件名。下面的文件名可变返回此正文部分字符串,而不是过多的文件名。我似乎无法找出我要去哪里错了。任何帮助是AP preciated。

I'm uploading files using the ASP.NET Web API. I've done this before the RC but for some reason the file is being saved as "BodyPart_3ded2bfb-40be-4183-b789-9301f93e90af" instead of the file name. The filename variable below returns this bodypart string too instead of the file name. I can't seem to figure out where I'm going wrong. Any help is appreciated.

客户端code:

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
    });
}; 

控制器:

    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}.", folder))
          };

        }, TaskScheduler.FromCurrentSynchronizationContext());

这些文件看起来像:

The files are looking like:

推荐答案

这是我们做了一个Concious酒店的改变 - 它被认为是安全风险采取在Content-Disposition头域提供的文件名等,而不是我们现在计算一个文件名,这是你所看到的。

That was a concious change we made -- it was considered a security risk to take the file name provided in the Content-Disposition header field and so instead we now compute a file name which is what you are seeing.

如果你想控制服务器的本地文件名自己,那么你可以从MultipartFormDataStreamProvider派生并重写GetLocalFileName提供任何你想要的名称。但是请注意,可能有安全方面的考虑这样做。

If you want to control the server local file name yourself then you can derive from MultipartFormDataStreamProvider and override GetLocalFileName to provide whatever name you want. Note though that there may be security considerations doing so.

希望这有助于

亨里克

这篇关于的ASP.NET Web API文件保存为&QUOT; BodyPart_3ded2bfb-40BE-4183-b789-9301f93e90af&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆