2013-03-11 25 views
0

我試圖從瀏覽器上傳文件並將其複製到URL文件夾如何上傳文件到url

使用c sharp。

(我擁有所有權限到這個文件夾)

我沒有problam文件上傳到我的硬盤驅動器

這樣的:

HttpPostedFileBase myfile; 

var path = Path.Combine(Server.MapPath("~/txt"), fileName); 

myfile.SaveAs(path); 

我已經嘗試將其上傳到這樣的網址,但我得到一個例外

HttpPostedFileBase myfile; 

var path =VirtualPathUtility.ToAbsolute("http://localhost:8080/game/images/"+fileName); 

myfile.SaveAs(path); 

例外:

System.ArgumentException: The relative virtual path 'http:/localhost:8080/game/images/ a baby bottle. Jpg' is not allowed here. 
    In - System.Web.VirtualPath.Create (String virtualPath, VirtualPathOptions 
+0

能否請您發佈文本從例外情況來看,我們其他人也可以從中學習? – kush 2013-03-11 13:31:43

回答

2

無法將文件上傳到遠程位置。如果你想這樣做,你將不得不修改遠程服務器,以便它接受文件上傳,這與服務器接受文件上傳的方式相同,然後使用WebClient發送HTTP請求到它。您不能使用SaveAs方法,因爲它需要本地路徑。

你可以有以下控制措施:

[HttpPost] 
public ActionResult Upload(HttpPostedFileBase myFile) 
{ 
    if (myFile != null && myFile.ContentLength > 0) 
    { 
     var fileName = Path.GetFileName(myFile.FileName); 
     var path = Path.Combine(Server.MapPath("~/App_Data"), fileName); 
     myFile.SaveAs(path); 
    }  

    ... 
} 

和相應表格與文件輸入:

@using (Html.BeginForm("Upload", null, FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <input type="file" name="myFile" /> 
    <button type="submit">Click this to upload the file</button> 
} 
0

您應該使用Server.MapPath("Path")

var path = Server.MapPath("~/images/") + fileName); 
myfile.SaveAs(path);