.net webapi重命名上传的文件 [英] .net webapi rename uploaded files

查看:125
本文介绍了.net webapi重命名上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一个用于将多个文件上传到webapi控制器的代码示例。

现在我不知道如何重命名为在服务器上创建的文件,因为这些文件的命名如下:

Hi, I found a code sample for uploading multiple files to a webapi controller.
Now I am not sure how to rename to files created on the server, as the files are named like this:

BodyPart_003fa560-908b-4d94-bed0-ac0532a09a66



这是代码示例的样子:


This is how the code sample looks:

string root = HttpContext.Current.Server.MapPath("~/Upload/" + IncidentId);
            var provider = new MultipartFormDataStreamProvider(root);

            try
            {
                StringBuilder sb = new StringBuilder(); // Holds the response body 

                // Read the form data and return an async task. 
                await Request.Content.ReadAsMultipartAsync(provider);

                // This illustrates how to get the form data. 
                foreach (var key in provider.FormData.AllKeys)
                {
                    foreach (var val in provider.FormData.GetValues(key))
                    {
                        sb.Append(string.Format("{0}: {1}\n", key, val));
                    }
                }

                // This illustrates how to get the file names for uploaded files. 
                foreach (var file in provider.FileData)
                {
                    sb.Append(string.Format("Uploaded file: {0} ({1} bytes)\n", fileInfo.Name, fileInfo.Length));
                }
                return new HttpResponseMessage()
                {
                    Content = new StringContent(sb.ToString())
                };
            }
            catch (System.Exception e)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
            }





是的,我知道上传文件夹可以公开访问。这仅用于测试目的。



所以问题是:如何重命名文件,或者有没有办法直接保存文件HTTP POST中提供的正确名称?



Yes, i know that the Upload folder will be publicly accessible. This is for testing purposes only at the moment.

So the question is: how do I rename the files, or is there a way to save the files directly with the correct name provided in the HTTP POST?

推荐答案

这篇关于.net webapi重命名上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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