protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/upload/") + FileUpload1.FileName);
uploadvalid.IsValid = false;
uploadvalid.ErrorMessage = "Your file was uploaded successfully.";
}
else
{
uploadvalid.IsValid = false;
uploadvalid.ErrorMessage = "You did not specify a file to upload.";
}
}
雖說從提示方框中是這樣區分
FileUpload1.PostedFile.SaveAs("放文件名稱")
FileUpload1.SaveAs("完整路徑")
但我只放文件名稱的話還是會有問題
放完整路徑才OK
另外因為開發環境跟實際上線環境當然不一樣
所以完整路徑自然不能直接寫"C:/XXX"
要用到另一個方法Server.MapPath
將相對路徑轉換成絕對路徑
--
參考網站
http://blog.xuite.net/sunnysoap/r/16996207
http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.fileupload.saveas%28v=vs.90%29.aspx#Y960
http://www.dotblogs.com.tw/mis2000lab/archive/2008/04/24/3455.aspx
沒有留言:
張貼留言