2011-05-27 81 views
0

使用<input type="file" name="fileUpload">我可以通過使用Request.MapPath並將其存儲在字符串中來獲取文件路徑。但是,當我做的:從<input type =「file」>獲取路徑&附加到電子郵件

string file = Request.MapPath(Request.Form["fileUpload"]); Attachment.Add(new Attachment(file));

我得到「找不到路徑的一部分」的錯誤。在獲取文件或將文件附加到MailMessage對象時缺少什麼?

+0

這是一個安全問題。 – 2011-05-27 03:40:59

+0

我不知道你的應用程序,但通常客戶端(瀏覽器)和服務器(webapp)在不同的主機上。您無法直接訪問其他主機上的文件。特殊的上傳文件控制用於此目的。 – artplastika 2011-05-27 03:41:51

+0

@Alex R. - 如果是這樣,是否有一個屬性,我應該改變我的控制器操作? – 2011-05-27 03:44:40

回答

3

我相信你不能這樣做,因爲該文件沒有被寫入服務器磁盤;也就是說,它被緩存在內存中。試試這個:

var destination = Path.GetTempFileName(); // you should probably replace this with a directory the IIS Worker Process has write permission to 
try { 
Request.Files[0].SaveAs(destination); 

Attachment.Add(new Attachment(destination)); 
// Send attachment 
} finally { 
File.Delete(destination); 
} 
相關問題