duanmei1885
2018-06-21 05:48
浏览 87

无法使用c#WebClient和php FILES上传文件

The following C# code is used to upload the json file (~3.6 MB) to the server. Here, I am using WebClient to upload file to the server.

 private void btnUploadToServer_Click(object sender, EventArgs e)
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    string filePath = @"C:\Users\SAKTHY-PC\Desktop\app_erp_suneka.json";
                    var serverPath = new Uri(@"http://example.com/newSync/upload.php");
                    client.UploadFile(myUri,filePath);
                }

                   Application.Exit();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

And I have a php script file (upload.php) in the following folder http://example.com/newSync/

<?php
    $filepath = $_FILES["file"]["tmp_name"];
    move_uploaded_file($filepath,"app_erp_suneka.json");
?>

The problem is unable to upload 2MB or more than 2MB file to the server. But less than 2MB file is successfully uploaded.

图片转代码服务由CSDN问答提供 功能建议

以下C#代码用于将json文件(~3.6 MB)上传到服务器。 在这里,我使用WebClient将文件上传到服务器。

  private void btnUploadToServer_Click(object sender,EventArgs e)
 {
 try 
 {
 using(WebClient client = new WebClient())
 {
  string filePath = @“C:\ Users \ SAKTHY-PC \ Desktop \ app_erp_suneka.json”; 
 var serverPath = new Uri(@“http://example.com/newSync/upload.php”); 
 client  .UploadFile(myUri,filePath); 
} 
 
 Application.Exit(); 
} 
 catch(异常错误)
 {
 MessageBox.Show(err.Message); 
} 
  } 
   
 
 

我在以下文件夹 http:// example中有一个php脚本文件( upload.php )。 com / newSync /

 &lt;?php 
 $ filepath = $ _FILES [“file”] [“tmp_name”]; 
 move_uploaded_file($ filepath  ,“app_erp_suneka.json”); 
?&gt; 
   
 
 

问题是无法上传2MB或超过2MB的fil e到服务器。 但成功上传的文件少于2MB。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyong8071 2018-06-21 06:40
    已采纳

    You need to check and increase theese variables on your PHP server (php.ini file):

    post_max_size => 8M
    upload_max_filesize => 2M
    

    Check it in phpinfo() or in server console:

    php --info | grep upload_max_filesize
    php --info | grep post_max_size
    php --info | grep php.ini <-- shows where php.ini is
    

    And control your php-log -- all errors and warnings are shown here:

    php --info | grep error_log <-- where error_log is
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题