![](/img/trans.png)
[英]Save file uploaded to server as binary string from javascript to file using c#
[英]No result string when a file is uploaded to PHP server using C# client
我有一個PHP代碼可以從C#客戶端上傳文件:
PHP:
<?php
if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
move_uploaded_file($tmp_name, "$name");
}
?>
C#:
WebClient Client = new WebClient();
Client.Headers.Add("Content-Type", "binary/octet-stream");
byte[] result = Client.UploadFile("https://products.softsolutionslimited.com/php.new/marketing/upload.php", "POST",
@"c:\file.zip");
string s = Encoding.UTF8.GetString(result, 0, result.Length);
它工作正常,文件已成功上傳,但是在byte[] result
變量中得到了空響應。 我可以從UploadFile呼叫中得到答復嗎? 否則,我要檢查文件是否存在,並使用以下代碼匹配大小等:
WebRequest Request = HttpWebRequest.Create("https://products.softsolutionslimited.com/php.new/marketing/Author.png");
Request.Method = "HEAD";
Request.Credentials = System.Net.CredentialCache.DefaultCredentials;
using (HttpWebResponse Response = (HttpWebResponse)Request.GetResponse())
{
if (Response.StatusCode != HttpStatusCode.OK)
{
MessageBox.Show("error");
return;
}
}
但是,如果我無法從UploadFile調用中獲得直接結果,則似乎會產生額外的開銷。
是否可以通過API調用獲取服務器上已保存文件的URL路徑?
WebClient UploadFile依賴於HTTP發布方法,因此基本上可以實現。 但是您沒有在PHP代碼中返回任何內容。
在php中返回文本
echo 'text to return';
WebClient.UploadFile
返回HTTP請求的響應主體。 目前,您的PHP代碼未產生任何輸出。
只需從您的PHP代碼返回某種輸出,您就會在響應中看到它。 您可以選擇任何確認。 例如:
echo $name;
(就我個人而言,我更喜歡對JSON進行API請求/響應,但總體概念是相同的。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.