Niranjan Godbole
Niranjan Godbole

Reputation: 2175

How to rename file in c# when uploading?

I am developing one application in web api and angularjs. I have file upload part. I am able to upload files and i am not storing files in webroot(i created folder called Uploads). My problem is i am not using any good naming convention to maintain uniqueness of files so there are chances of overriding files. I am new to angularjs so i refered below link. http://instinctcoder.com/angularjs-upload-multiple-files-to-asp-net-web-api/ This is my controller level code.

if (!Request.Content.IsMimeMultipartContent())
{
    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
var uploadPath = HttpContext.Current.Server.MapPath("~/Uploads");
var multipartFormDataStreamProvider = new CustomUploadMultipartFormProvider(uploadPath);
await Request.Content.ReadAsMultipartAsync(multipartFormDataStreamProvider);
var fileName = "";
DateTime dt = DateTime.Now;
foreach (var key in multipartFormDataStreamProvider.Contents)
{
    var a = key.Headers;
    fileName = a.ContentDisposition.FileName;
    break;
}
foreach (var key in multipartFormDataStreamProvider.FormData.AllKeys)
{
    foreach (var val in multipartFormDataStreamProvider.FormData.GetValues(key))
    {
        Console.WriteLine(string.Format("{0}: {1}", key, val));
    }
}

In the above code I am trying to add date part to beginning of file name as below

string filenameNew = "App1" + DateTime.Now.ToString("yyyyMMddHHmmss");
fileName = filenameNew + a.ContentDisposition.FileName; 

public CustomUploadMultipartFormProvider(string path) : base(path) { }
public override string GetLocalFileName(HttpContentHeaders headers)
{
    string startwith = "Nor" + DateTime.Now.ToString("yyyyMMddHHmmss");
    if (headers != null && headers.ContentDisposition != null)
    {
        return headers
            .ContentDisposition
            .FileName.TrimEnd('"').TrimStart('"').StartsWith("startwith").ToString();
    }
    return base.GetLocalFileName(headers);
}

This i tried but whatever the original file name that only comes. May I get some idea where can i append datepart to file while saving? Any help would be appreciated. Thank you.

Upvotes: 1

Views: 1793

Answers (1)

caesay
caesay

Reputation: 17213

I'm not sure what you're trying to do inside of the GetLocalFileName, this is pretty messed up.

First off, StartsWith returns a boolean (true or false) that indicates if the string starts with whatever you put in the parenthesis.

string str = "SIMPLE";
bool t = str.StartsWith("SIM"); // true
bool f = str.StartsWith("ZIM"); // false

The fact you're turning this bool back into a string and also passing the string "startsWith" into the method, means it will always return the string "false" (a bool value converted into a string) unless the real filename starts with "startsWith".

I think this is what you're looking for:

public override string GetLocalFileName(HttpContentHeaders headers)
{
    string prefix = "Nor" + DateTime.Now.ToString("yyyyMMddHHmmss");
    if (headers != null && headers.ContentDisposition != null)
    {
        var filename = headers.ContentDisposition.FileName.Trim('"');
        return prefix + filename;
    }
    return base.GetLocalFileName(headers);
}

My suggestion for you is to learn the basics of C# and .Net a bit more, maybe read a C# book or something.

Upvotes: 3

Related Questions