我有一個問題 - 我想上傳圖像到我的項目上的本地目錄ASP.NET
C#
,我已經成功。重命名文件上傳到服務器ASP.NET
但現在我需要重新命名文件,然後將它們上傳到我的本地目錄。
我該如何做到這一點?
我有一個問題 - 我想上傳圖像到我的項目上的本地目錄ASP.NET
C#
,我已經成功。重命名文件上傳到服務器ASP.NET
但現在我需要重新命名文件,然後將它們上傳到我的本地目錄。
我該如何做到這一點?
你會不會得到直接Rename
方法重命名文件名.....相反,你可以使用Move
方法像在以下方式重命名:
System.IO.File.Move(oldFile, newFile);
或者,你可以使用Copy
方法太:
System.IO.File.Copy(oldFile, newFile);
System.IO.File.Delete(oldFile);
其簡單的給當u將它保存到本地
string newname = "yournewname";
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Path.Combine(uploadFolder + newname+ extension));
一個新的文件名
您應該使用'Path.Combine'而不是'uploadFolder + newname + extension'。 –
它會是這樣的
protected void button1_Click(object sender, EventArgs e){
string directory = Server.MapPath("uploads");
string fExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string fileName = "newFileName" + fExtension;
this.FileUpload1.SaveAs(Path.Combine(directory,fileName));
}
您應該使用'Path.Combine'而不是'directory + @「/」+ fileName'。 –
@TimSchmelter你說得對,我已經更新了代碼,謝謝。 –
你手動上傳? – ChaosPandion