回答

1

如果你的問題是「如何保存爲完整路徑」,請試試這個

string path = Path.Combine(Server.MapPath("~/Content/img/Company/LogoFiles"), filename); 

如果你的問題是「如何保存爲相對路徑」,請試試這個

[HttpPost] 
public ActionResult Save(HttpPostedFileBase myFile) 
{ 
    string filename = myFile.FileName; 
    string relativeFileName = "~/Content/Uploads/" + filename;// relative path 

    return View(); 
} 
+0

不,我想保存相對路徑,但在數據庫完整路徑被保存 –

+0

我編輯我的答案,請看看。 –

0

此代碼不允許保存相對路徑,當您嘗試時,將設置文件名的全部內容,而不僅僅是文件名。如果您在此處插入斷點,則必須獲得類似於以下內容的內容:

"/content/Uploads/C:Document/Picture/father.jpeg" 

,而不是你會希望它像

"/content/Uploads/father.jpeg" 
0

爲了保存文檔的相對路徑的相對路徑,你應該在下面的示例中使用system.io的財產的getFileName作爲表演。

我想你想這樣做在你的控制器的POST方法包含在我的例子模型或httppostedfilebase ,我有httppostedfilebase屬性命名imagefichier模型,這樣你就可以這樣做,得到的文件名

var lefilename = System.IO.Path.GetFileName(model.imagefichier.FileName); 
//Suppose that you want to save it in your root web file 
//like ~/Images/Administrateur/leilename,you can do it like this 
    string cheminbdd = "~/Images/Administrateur/" + lefilename; 
    //it is cheminbdd that you can save in your database as your relative path