![](/img/trans.png)
[英]How to save the SharePoint File Upload Link in C# MVC Project?
[英]How save uploaded file? c# mvc
我想將圖像文件上傳到項目的文件夾,但我的捕獲出錯:找不到路徑“C:\\project\\uploads\\logotipos\\11111\\”的一部分。
我做錯了什么? 我想將我的客戶端上傳的圖像保存在該文件夾中...該文件夾存在...啊,如果我為 folder_exists3 設置一個斷點,顯示我的真實值!
我的代碼是:
try
{
var fileName = dados.cod_cliente;
bool folder_exists = Directory.Exists(Server.MapPath("~/uploads"));
if(!folder_exists)
Directory.CreateDirectory(Server.MapPath("~/uploads"));
bool folder_exists2 = Directory.Exists(Server.MapPath("~/uploads/logo"));
if(!folder_exists2)
Directory.CreateDirectory(Server.MapPath("~/uploads/logo"));
bool folder_exists3 = Directory.Exists(Server.MapPath("~/uploads/logo/" + fileName));
if(!folder_exists3)
Directory.CreateDirectory(Server.MapPath("~/uploads/logo/"+fileName));
file.SaveAs(Server.MapPath("~/uploads/logo/" + fileName+"/"));
}
catch(Exception e)
{
}
有人知道我做錯了什么嗎?
謝謝 :)
嘗試這個:
string targetFolder = HttpContext.Current.Server.MapPath("~/uploads/logo");
string targetPath = Path.Combine(targetFolder, yourFileName);
file.SaveAs(targetPath);
您的錯誤如下:
bool folder_exists3 = Directory.Exists(Server.MapPath("~/uploads/logo/" + fileName));
if(!folder_exists3)
Directory.CreateDirectory(Server.MapPath("~/uploads/logo/"+fileName));
您檢查目錄是否存在,但您應該檢查文件是否存在:
File.Exists(....);
你需要文件名
file.SaveAs(Server.MapPath("~/uploads/logo/" + fileName+"/" + your_image_fillename));
刪除路徑的最后一部分以保存您有一個額外的“/”
它應該是
file.SaveAs(Server.MapPath("~/uploads/logo/" + fileName);
您也沒有設置文件擴展名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.