user5402143
user5402143

Reputation:

get the name of a file without empty spaces

I´m working in a MVC project and receive a file(HttpPostedFileBase property) in my controller via modelbinding and what I want is to delete all the empty spaces in the name of the file I just received, for that purpose I use this

 var nombre_archivo = DateTime.Now.ToString("yyyyMMddHHmm.") +"_"+ (info.file.FileName.ToString()).Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

but the var "nombre_archivo" is always: 201801240942.System.String[] and what I want is 201801240942.nameOfFile, could you please tell me where is the error?

Upvotes: 1

Views: 906

Answers (4)

Florian
Florian

Reputation: 1481

This is how you should be able to fix your problem

string nombre_archivo = string.Format("{0}_{1}", DateTime.Now.ToString("yyyyMMddHHmm."), info.file.FileName.Where(c => !Char.IsWhiteSpace(c))

EDIT : You should use string.Replace instead of using Linq query. There is a format problem I did not expect.

So, the right answer is given below, but basically, it would look like :

string nombre_archivo = string.Format("{0}_{1}", DateTime.Now.ToString("yyyyMMddHHmm."), info.file.FileName.Replace(" ", ""));

Upvotes: -2

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131189

You can use String.Replace to replace a space with an empty string. The method has other issues though. It doesn't check whether FileName is valid which means someone could make a POST request with a hand-coded path like ../../ or E:\somepath\myinnocentprogram.exe to write a file to the server's disk. Or worse, ../index.htm.

Replacing spaces doesn't make much sense. It's the dots and slashes that can result

If you check Uploading a File (Or Files) With ASP.NET MVC you'll see that the author uses Path.GetFileName() to retrieve only the file's name before saving it in the proper folder. Your code should look like this::

[HttpPost]
public ActionResult Index(HttpPostedFileBase file) {

  if (file.ContentLength > 0) {
    var fileName = Path.GetFileName(file.FileName)
                       .Replace(" ","");
    var finalName=String.Format("{0:yyyyMMddHHmm}._{1}",DateTime.Now,fileName);
    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), finalName);
    file.SaveAs(path);
  }

  return RedirectToAction("Index");
}

This ensures that only the filename part of the file is used, and that the file is saved in the appropriate folder, even if someone posted an invalid path

Upvotes: 2

Felipe Deveza
Felipe Deveza

Reputation: 1959

var name = $"{DateTime.Now.ToString("yyyyMMddHHmm.")}_{info.file.FileName.Replace(" ", "")}";

Upvotes: 2

GGO
GGO

Reputation: 2748

Your are splitting on an array of dots.

Use replace instead :

var nombre_archivo = string.Format("{0}_{1}",
                         DateTime.Now.ToString("yyyyMMddHHmm."),
                         info.file.FileName.replace(" ", "")
);

Moreover, we recommend to use string.Format instead of + concatenation. It's faster and clearer

Upvotes: 2

Related Questions