2013-03-11 7 views
0

브라우저에서 파일을 업로드하고 URL 폴더에 복사하려고합니다. C sharp를 사용하여url에 파일을 업로드하는 방법

입니다.

(내가이 폴더에 대한 모든 권한을 가지고)

내가 더 problam이처럼 내 하드 드라이브

에 파일을 업로드하지 있습니다

HttpPostedFileBase myfile; 

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

myfile.SaveAs(path); 

을 난에 업로드하려고했다 이 URL은 예외이지만 예외가 발생합니다.

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

를 사용한다 : 파일 입력과

[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); } ... } 

및 해당 양식을 예외적으로 우리는 나머지 사람들도 그것으로부터 배울 수 있습니까? – kush

답변

2

파일을 원격 위치에 업로드 할 수 없습니다. 이 기능을 사용하려면 원격 서버를 수정하여 서버가 파일 업로드를 허용하는 것과 동일한 방법으로 파일 업로드를 허용 한 다음 WebClient을 사용하여 HTTP 요청을 보내야합니다. 로컬 경로를 예상하므로 SaveAs 메서드를 사용할 수 없습니다.

다음과 같은 컨트롤러 액션 수 :

@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); 
관련 문제