2011-09-14 5 views
0

파일을 서버에 업로드하고 싶습니다. 나는 (내가 서버를 WAMP 사용하고 있습니다) 로컬 호스트 서버에 파일을 업로드하는이 기능을 썼다Httpwebrequest를 사용하여 파일 업로드

Additional information: The remote server returned an error: (405) Method Not Allowed.

내가 사용하려고 : 내가 버튼을 클릭하면

private void button1_Click_1(object sender, EventArgs e) 
    { 
     FileStream fstream = new FileStream(@"C:\Users\Albert\Documents\10050409_3276.doc", FileMode.OpenOrCreate); 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/upload_file"); 
     request.Method = "PUT"; 
     request.ContentLength = fstream.Length; 
     request.AllowWriteStreamBuffering = true; 
     Stream request_stream = request.GetRequestStream(); 
     byte[] indata = new byte[1024]; 
     int bytes_read = fstream.Read(indata, 0, indata.Length); 
     while (bytes_read > 0) 
     { 
      request_stream.Write(indata, 0, indata.Length); 
      bytes_read = fstream.Read(indata, 0, indata.Length); 
     } 
     fstream.Close(); 
     request_stream.Close(); 
     request.GetResponse(); 
     MessageBox.Show("ok"); 
    } 

그래서 예외 apper는 말했다 "POST"대신 "PUT"프로그램이 작동하고 메시지 상자가 'ok'라고 표시되지만 localhost-> upload_file (folder)을 열면 Ididn't 파일을 찾습니다.

나는 내 프로그램을 테스트했지만 문제가 발생했습니다.

실제 서버로 프로그램을 테스트하고 네트워크 자격 증명을 입력하고 (777) 권한 => 문제가 발생한 폴더에 업로드를 시도했습니다.

문제가 정확히 어디에서 발생합니까?

감사합니다 :)

+0

그건 바로, C# 코드입니까? –

+0

네, C# 코드입니다 – Albert

+0

Multipart Mime 타입이 누락 된 것 같아요. 또한 "PUT"방법을 허용하는 웹 사이트를 특별히 만들지 않는 한 반드시 "POST"를 사용해야합니다. –

답변

2

(내가 노력하지 않았거나 일부 코드)

WebClient client = new WebClient(); 
byte[] bret = client.UploadFile(path, "POST", FilePath); 
//path==URL 
//FilePath==Your uploading file path 

또는

WebClient webClient = new WebClient(); 
string webAddress = null; 
try 
{ 
    webAddress = @"http://localhost/upload_file/"; 

    webClient.Credentials = CredentialCache.DefaultCredentials; 

    WebRequest serverRequest = WebRequest.Create(webAddress); 
    serverRequest.Credentials = CredentialCache.DefaultCredentials; 
    WebResponse serverResponse; 
    serverResponse = serverRequest.GetResponse(); 
    serverResponse.Close(); 

    webClient.UploadFile(path, "POST", FilePath); 
    webClient.Dispose(); 
    webClient = null; 
} 
catch (Exception error) 
{ 
    MessageBox.Show(error.Message); 
} 

웹 클라이언트와 시도

+0

WebClient 클라이언트 = 새 WebClient(); byte [] bret = client.UploadFile ("http : // localhost/Lecture Fetcher/xml_files", "POST", "k.xml"); Console.WriteLine ("ok"); – Albert

+0

consol 쓰기 (확인)하지만 xml_files (폴더)를 열면 파일이 존재하지 않습니다. – Albert

+0

업데이트 된 코드를 시도하십시오. – deepi

관련 문제