ShodowOverRide
ShodowOverRide

Reputation: 471

Change The name While saving the File on .net server using mvc4

I want to upload an image to my server using android device i google for it and found a code here it is below

public async Task<HttpResponseMessage> PostFile()
{
    // Check if the request contains multipart/form-data.
    if (!Request.Content.IsMimeMultipartContent())
    {
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

    //string root = HttpContext.Current.Server.MapPath("~/App_Data");
    string root = HttpContext.Current.Server.MapPath("~/Files");
    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)
        {
           // Trace.WriteLine(key.Headers.ContentDisposition.FileName);
            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)
        {
            FileInfo fileInfo = new FileInfo(file.LocalFileName);
            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);
    }
}

This code works fine, which when i send a file from an android cell phone it save it in my server specified folder, but now the problem is that when i try to access it from another cell phone it shows content not, Why?? content not found is due to that when this code saves the file on my server it changes its original name and replace it with a name like this

BodyPart_2ce1d675-31f6-41ec-8cdd-595fea6af0e3

and remove its extension from it so that why its not accessible, but instead of it if i rename this file on my server and add the extension to it than it is accessible, here my point is that is there any way to store a file on server with name plus(+) extension using this code with some small modification.

Thanks in advance for your help.

Upvotes: 0

Views: 1045

Answers (1)

User 12345678
User 12345678

Reputation: 7804

You could extend the MultipartFormDataStreamProvider class in order to define your own file naming logic like this:

using System.Net.Http;
using System.Net.Http.Headers;

...

public class CustomMultipartFormDataStreamProvider 
    : MultipartFormDataStreamProvider
{
    public CustomMultipartFormDataStreamProvider(string path) 
        : base(path)
    {
    }

    public override string GetLocalFileName(HttpContentHeaders headers)
    {
        return headers.ContentDisposition.FileName.Replace("\"", string.Empty);
    }
}

Now replace the occurence of CustomMultipartFormDataStreamProvider so that

var provider = new MultipartFormDataStreamProvider(root);

becomes

var provider = new CustomMultipartFormDataStreamProvider (root);

Upvotes: 2

Related Questions