2014-01-22 61 views
1

該方案是將文件從server1(本地服務器)中的PHP應用程序發送到server2中的C#.net應用程序。無法使用cURL發佈文件

使用簡單的HTML表單發送文件時,目標服務器收到它沒有問題。

使用cURL發送該文件時,我從目標服務器收到錯誤500 請注意,我無法訪問目標服務器,並且其管理員確信一切正常,因爲HTML表單已成功提交。

這裏是我的代碼後的文件:

$filedata = $fileName; // Absolute path of the file ==> 'D:\wamp\www\my_project\upload\image\201401070657414.jpg' 
$headers = array("Content-Type:multipart/form-data"); 
$postfields = array("filedata" => "@$filedata", "filename" => $fileName); 
$ch = curl_init(); 
$options = array(
    CURLOPT_URL => 'http://server2/postphoto.aspx', //500 
    CURLOPT_HEADER => true, 
    CURLOPT_POST => 1, 
    CURLOPT_HTTPHEADER => $headers, 
    CURLOPT_POSTFIELDS => $postfields, 
    CURLOPT_INFILESIZE => filesize($filedata), 
    CURLOPT_RETURNTRANSFER => true 
); 
curl_setopt_array($ch, $options); 
$response = curl_exec($ch); 

而且curl_getinfo($ CH)返回:

array (size=26) 
    'url' => string 'http://server2/postphoto.aspx' (length=41) 
    'content_type' => string 'text/html; charset=utf-8' (length=24) 
    'http_code' => int 500 
    'header_size' => int 265 
    'request_size' => int 203 
    'filetime' => int -1 
    'ssl_verify_result' => int 0 
    'redirect_count' => int 0 
    'total_time' => float 0.032 
    'namelookup_time' => float 0 
    'connect_time' => float 0 
    'pretransfer_time' => float 0 
    'size_upload' => float 28998 
    'size_download' => float 7127 
    'speed_download' => float 222718 
    'speed_upload' => float 906187 
    'download_content_length' => float 7127 
    'upload_content_length' => float 28998 
    'starttransfer_time' => float 0.016 
    'redirect_time' => float 0 
    'certinfo' => 
    array (size=0) 
     empty 
    ... 

什麼是錯我的代碼?

變量$迴應表示:

string 'HTTP/1.1 100 Continue 

HTTP/1.1 200 OK 
Date: Wed, 22 Jan 2014 08:31:54 GMT 
Server: Microsoft-IIS/6.0 
X-Powered-By: ASP.NET 
X-AspNet-Version: 2.0.50727 
Cache-Control: private 
Content-Type: text/html; charset=utf-8 
Content-Length: 6 

nofile' (length=249) 

Content-Type的rturns text/html的同時我已經把它在我的頭爲multipart/form-數據。

以下是在server2的Asp.net代碼:

if (Request.Files.Count == 0) 
    { 
     Response.Write("nofile"); 
     return; 
    } 
    System.Web.HttpPostedFile file = Request.Files[0]; 

    System.IO.Stream streamObj = file.InputStream; 
    Byte[] buffer = new Byte[file.ContentLength]; 
    streamObj.Read(buffer, 0, buffer.Length); 
    streamObj.Close(); 
    streamObj = null; 


    System.IO.File.WriteAllBytes("d:\\photo\\" + file.FileName, buffer); 
+0

你應該發佈'c#'代碼。 –

+0

我也用php測試過,得到了同樣的錯誤。 –

+0

向我們展示「simple html form」的HTML源代碼,它與瀏覽器 –

回答

0

嘗試分配變量以curl_exec($ CH);像這樣$response = curl_exec($ch);並做一個var_dump var_dump($response)。告訴我們服務器的迴應是什麼。

+0

字符串「HTTP/1.1 100繼續 HTTP/1.1 200 OK 日期:星期三,2014年1月22日8點26分38秒GMT 服務器:Microsoft-IIS/6.0 X供電,通過:ASP.NET X- AspNet-Version:2.0.50727 Cache-Control:private Content-Type:text/html; charset = utf-8 內容長度:6 nofile'(長度= 249) –

+0

您發佈的結果被截斷。嘗試做一個回聲。 –

+0

我編輯了問題並添加了回覆 –

-1

這個工程,相應地更改變量您的需求:

<?php 
     $post_data['content'] = "@/c:/xampp/htdocs/site/a.doc"; 
     $post_data['filename'] = "a.doc"; 

     foreach ($post_data as $key => $value) 
     { 
      $post_items[] = $key . '=' . $value; 
     } 

     $post_string = implode ('&', $post_items); 



    //create cURL connection 
$curl_connection = curl_init('https://yoururl.com/form.php'); 

//set options 
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); 
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); 
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); 

//set data to be posted 
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data); 

//perform our request 
$result = curl_exec($curl_connection); 

echo $result; 

//close the connection 
curl_close($curl_connection); 

?> 

欲瞭解更多信息請檢查該link

+0

這是一個簡單的複製和粘貼從谷歌沒有解釋,沒有驗證。 – DanFromGermany

+0

@DanFromGermany,誰告訴你我沒有測試過代碼?我已經完成了,猜猜是什麼?有用。 –

+0

不過,你不解釋**爲什麼**你的代碼有效,但是OP沒有。 – DanFromGermany

0

我覺得CURLOPT_INFILESIZE僅用於PUT請求。取而代之的是,明確地設置POST:

CURLOPT_CUSTOMREQUEST => POST 
CURLOPT_POST   => true 

我也建議"filedata" => "@" . $filedata - 也不清楚。

下面是一些錯誤:

$filedata = $fileName; // Absolute path of the file 

他們可以是不一樣的,一個包含完整的絕對路徑的文件,可能是/tmp/phpcpjNeQ,另一個包含文件名沒有路徑test_file.csv

要驗證您的本地文件是否可訪問,請使用fopen()

assert(is_resource(fopen($filedata, 'r'))); 
+0

assert函數返回true –

+0

$ filedata是這樣的:'D:\ wamp \ www \ my_project \ upload \ image \ 201401070657414.jpg' –

0

嘗試使用PHP來處理請求給出了一個文件,數組如下:

Array 
(
    [filedata] => Array 
     (
      [name] => 201401070657414.jpg 
      [type] => application/octet-stream 
      [tmp_name] => D:\wamp\tmp\php519A.tmp 
      [error] => 0 
      [size] => 28619 
     ) 

) 

那麼什麼是這裏的錯誤!?