[英]Rename the uploaded file name C#
我有以下C#代碼:
SelectQuery = string.Format("SELECT UserID from tblUsers WHERE Email='{0}'", Email);
ds = DbQ.ExecuteQuery("SiteDB.mdb", SelectQuery);
string UserID = ds.Tables[0].Rows[0]["UserID"].ToString();
if (Request.ContentLength != 0)
{
int Size = Request.Files[0].ContentLength / 1024;
if (Size <= 512)
{
string LocalFile = Request.Files[0].FileName;
int LastIndex = LocalFile.LastIndexOf(@"\") + 1;
File = LocalFile.Substring(LastIndex, LocalFile.Length - LastIndex);
// File = "ProfilePic-Id-" + UserID;
string Path = Server.MapPath("images/profiles/") + File;
Request.Files[0].SaveAs(Path);
}
else
{
Response.Write("The file is too big !");
}
}
else
{
Response.Write("Unknown Error !");
}
我希望上傳的文件名重命名為“ProfilePic-Id-”+ UserID,我在評論中嘗試但是它沒有用,我怎樣才能重命名上傳的文件名?
希望得到幫助,謝謝!
這樣的事情怎么樣:
if (Size <= 512)
{
string path = string.Format("~/images/profiles/ProfilePic-Id-{0}.{1}",
UserID, System.IO.Path.GetExtension(Request.Files[0].FileName));
Request.Files[0].SaveAs(Server.MapPath(path));
}
看,當你說I get some unrecognized file ...
,這很明顯,因為你沒有在它上面設置擴展名。 文件字節可能很好,它只是操作系統的一個未經識別的擴展(即它甚至沒有),所以你需要提供它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.