1

I am trying to change the file name of images to the value that I posted in the input box username. The files are getting uploaded to the server and also, after overriding GetLocalFileName the file name is changed from "BodyPart_(xyz)" to the original one. How do I rename them to the value that I provided in the input box?

<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>

I have stored the value that I received in the newName variable but I am confused on how to rename the file in the server.

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

1 Answer 1

1

One way is to override the ExecutePostProcessingAsync method like the following:

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I couldn't figure out how to copy the file with the new name and delete the old file. How do I implement that in the ExecutePostProcessingAsync() itself?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.