繁体   English   中英

ASP.NET WebAPI更改文件名?

[英]ASP.NET WebAPI change file name?

我正在尝试将图像的文件名更改为我在输入框username发布的值。 文件将被上传到服务器,并且在重写GetLocalFileName ,文件名也将从“ BodyPart_(xyz)”更改为原始文件。 如何将它们重命名为在输入框中提供的值?

<form name="form1" method="post" enctype="multipart/form-data" action="api/poster/postformdata">
            <div class="row-fluid fileform">
                <div class="span3"><strong>Username:</strong></div>
                <input name="username" value="test" type="text" readonly/>
            </div>

            <div class="row-fluid fileform">
                <div class="span3"><strong>Poster:</strong></div>
                <div class="span4"><input name="posterFileName" ng-model="posterFileName" type="file" /></div>
            </div>

            <div class="row-fluid fileform">
                 <div class="span8"><input type="submit" value="Submit" class="btn btn-small btn-primary submitform" /></div>
            </div>
</form>

我已经将收到的值存储在newName变量中,但是我对如何在服务器中重命名文件感到困惑。

public async Task<HttpResponseMessage> PostFormData()
            {
                if (!Request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }

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

                try
                {
                    await Request.Content.ReadAsMultipartAsync(provider);
                    // Show all the key-value pairs.
                    foreach (var key in provider.FormData.AllKeys)
                    {
                        foreach (var val in provider.FormData.GetValues(key))
                        {
                            Trace.WriteLine(string.Format("{0}: {1}", key, val));
                            newName = val;
                        }
                    }

                    return Request.CreateResponse(HttpStatusCode.OK);
                }
                catch (System.Exception e)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
                }
            }

            public class MyMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
            {
                public MyMultipartFormDataStreamProvider(string path)
                    : base(path)
                {

                }

                public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
                {
                    string fileName;
                    if (!string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName))
                    {
                        fileName = headers.ContentDisposition.FileName;
                    }
                    else
                    {
                        fileName = Guid.NewGuid().ToString() + ".data";
                    }
                    return fileName.Replace("\"", string.Empty);
                }
            }

一种方法是重写ExecutePostProcessingAsync方法,如下所示:

public override async Task ExecutePostProcessingAsync()
{
    await  base.ExecutePostProcessingAsync();

    // By this time the file would have been uploaded to the location you provided
    // and also the dictionaries like FormData and FileData would be populated with information
    // that you can use like below

    string targetFileName = FormData["username"];

    // get the uploaded file's name
    string currentFileName = FileData[0].LocalFileName;

    //TODO: rename the file
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM