Click here to Skip to main content
14,388,963 members
Rate this:
Please Sign up or sign in to vote.
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);
            }


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?
Posted
Updated 10-Apr-14 4:47am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100