2012-10-31 3 views
0

PHP 서버에서 C# 코드로 파일을 업로드합니다. 그러나 몇 가지 문제에 직면 해 있습니다.PHP 서버에서 사용하는 문자열로 C# 파일 업로드

먼저 나는을 UploadFile() 메서드를 호출하여 파일을 업로드하는 웹 클라이언트 개체를 사용하고 다음 코드에 의해 UploadString() 메서드를 호출하여 문자열로 업로드되었습니다

 String StoreID = "First Store"; 
     WebClient Client = new WebClient(); 
     String s = Client.UploadString("http://localhost/upload.php", "POST", StoreID); 
     Client.Headers.Add("Content-Type","binary/octet-stream"); 
     byte[] result = Client.UploadFile("http://localhost/upload.php", "POST", "C:\\aaaa.jpg"); 
     s = s + System.Text.Encoding.UTF8.GetString(result,0,result.Length); 

문제는 내가 두 번 요청하고 있음 그래서 문자열과 파일이 동시에 전송되지 않습니다. String 또는 File 중 하나를 받고 있습니다. 그러나 나는 동시에 두 가지가 필요하다. 나는 바이트 코드를 사용하고 나는 PHP에서 그것을 추출하는 방법을 알고 있기 때문에 UploadData()을 사용하고 싶지 않다.

문자열을 폴더 이름으로 지정하면 문자열과 파일을 보내야 파일이 PHP 서버의 지정된 폴더에 저장 될 수 있습니다.

내가 공부 한 WebRequest 및 WebResponse 개체가있는 솔루션이있을 수 있습니다. 하지만 C#으로 WebResponse를 사용하여 요청을 보내고 PHP에서 가져 오는 방법을 모릅니다.

제안 사항 !!!!

+0

참조 : http://stackoverflow.com/questions/2950292/how-to-upload-multiple-files-using-webclient-uploadfile-uploadvalues-in-c –

답변

0

이 시도 :

WebClient web = new WebClient(); 
try{ 

    web.UploadFile("http://" + ip + "/test.php", StoreID); 
} 
catch(Exception e) 
{ 
    MessageBox.Show("Upload failed"); 
} 

는 이제 PHP 파일에서 파일에 액세스 할 수 있습니다.

<?php 
//check whether the folder the exists 
if(!(file_exists('C:/Users/dhanu-sdu/Desktop/test'))) 
{ 
    //create the folder 
    mkdir('C:/Users/ComputerName/Desktop/test'); 
    //give permission to the folder 
    chmod('C:/Users/ComputerName/Desktop/test', 0777); 
} 

//check whether the file exists 
if (file_exists('C:/Users/ComputerName/Desktop/test/'. $_FILES["file"]["name"])) 
{ 
    echo $_FILES["file"]["name"] . " already exists. "; 
} 
else 
{ 
    //move the file into the new folder 
    move_uploaded_file($_FILES["file"]["tmp_name"],'C:/Users/ComputerName/Desktop/test/'. $_FILES["file"]["name"]); 

} 

?> 

또한, 당신은 PHP 서버에서 데이터를 다운로드하고 다음과 같은 코드를 사용하여 C#을 웹 브라우저에 표시 할 수 있습니다

WebClient web = new WebClient(); 
try{ 
    byte[] response = web.DownloadData("http://" + ip +"/test.php"); 
    webBrowser1.DocumentText = System.Text.ASCIIEncoding.ASCII.GetString(response); 
} 
catch(Exception e) 
{ 
    MessageBox.Show("Download failed"); 
} 
+0

나는 이것을 시도했다. 이것은 PHP 서버에만 파일을 업로드합니다. 하지만 File과 함께 문자열을 보내야합니다. 어떤 폴더 파일을 저장해야하는지 식별 할 수 있도록 문자열이 폴더의 이름이되도록합시다. – MouseCrasher

+0

@MouseCrasher in web.UploadFile ("http : //"+ ip + "/test.php", StoreID); 당신은 문자열을 보낼 수 있습니다. 또한 .zip 파일로 업로드하여 문자열 파일을 업로드 할 수도 있습니다. – aliboy38

+0

당신은 맞지만 제 경우에는'Client.UploadFile ("http : //localhost/upload.php", "POST", "C : \\ aaaa.jpg") 코드 만 보시면됩니다. Client.UploadString ("http : //localhost/upload.php", "POST", StoreID); 이들은 두 개의 요청이며 _aaaa.jpg_ 및 _StoreID_를 보내려는 요청을 하나만 호출하기를 원합니다. – MouseCrasher

0

당신은 파일을 받아 PHP와 웹 서비스를 만들 수 있습니다. 그런 다음 해당 웹 서비스를 게시하고 C# 참조를 추가 한 다음 파일을 수락하는 C# 코드 내에서 teh 메서드를 호출하고 vualá!

PHP와 SOAP을 만드는 방법을 link

+0

대체 솔루션입니다. 그러나 웹 서비스가 없으면 가능할 수 있습니다. – MouseCrasher

+0

왜 WS를 사용하고 싶지 않은지 잘 모르겠다면 많은 도움이 될 것입니다. – VicoMan

+0

브라우저가 양식을 제출하면 웹 서비스를 사용하지 않습니다. @MouseCrasher가하려고하는 것은 이것을 재현하는 것입니다. –