简体   繁体   中英

Saving Uploaded File with Unique Name in Folder

I need to save a file(image) to a folder. If have an image with name "OrignalName", then its saving in their original name in to my specified folder. I'm using

string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
GenerateFileName(filename);
fileupload1.SaveAs(Server.MapPath("Images" + filename));

How should i change the filename to a unique such as timestamp (yyyymmddMMss) Any help will be appreciated.

System.IO.FileInfo file = new System.IO.FileInfo(FileUpload1.PostedFile.FileName);
    string fname = file.Name.Remove((file.Name.Length - file.Extension.Length));
    fname = fname + System.DateTime.Now.ToString("_ddMMyyhhmmss") + file.Extension; 

您可以将文件名与当前日期和时间连接起来,然后再将其保存到数据库中,如下所示:

   string strtemp =  filename + System.DateTime.Now.ToString("ddMMyyhhmmss");

You can try with this code

var newPath = filename + DateTime.Now.ToString("yyyymmddMMss");
fileupload1.SaveAs(Server.MapPath("Images/" + newPath));   

Easily is save file to location and then rename him

string newName = System.DateTime.Now.ToString("ddMMyyhhmmss");
Microsoft.VisualBasic.FileIO.RenameFile(file, newName);

Try the following:

 int generatedNo = randomNumber.Next(100, int.MaxValue);
 string filePath=Path.Combine(Server.MapPath("~/finaldesign")+generatedNo+".jpg");
 imageTosave.Save(filePath, ImageFormat.Jpeg);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM