【问题标题】:PHP / CURL / ASP.NET / C# - Send files bigger than 9mbPHP / CURL / ASP.NET / C# - 发送大于 9mb 的文件
【发布时间】:2015-05-03 01:04:18
【问题描述】:

我想通过 CURL 从 PHP (v5.4) 将文件发送到 ASP.NET WebApi (C#)。我认为这应该很容易,但我遇到了一些我无法解释的奇怪行为:

php 脚本:

<?php   
    $files = array();
    $files['file_content'] = '@'.realpath('./myfile.jpg');

    $url = 'https://localhost:44307/api/v1/File';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, trim($url));

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
    curl_setopt($ch, CURLOPT_SSLVERSION, 3); 
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // ignore ssl verifyer
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    curl_exec($ch);

    echo 'ErrorMsg: '.curl_error($ch).'<br>';
    echo "ErrorNr.:".curl_errno($ch).'<br>';    

    curl_close($ch);
?>

php.ini:

upload_max_filesize = 1024M
max_file_uploads = 20
memory_limit = 1024M
post_max_size = 1024M

Web.config:

<system.web>
    <httpRuntime executionTimeout="1800" maxRequestLength="2097152" requestValidationMode="2.0" />
</system.web>
<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="2147483648" /> 
        </requestFiltering>
    </security>        
</system.webServer>

当我使用大于 9mb 的文件(例如两个 5mb 文件)执行脚本时,根据文件的大小,几分钟后我会得到以下结果:

ErrorMsg: SSL read: error:00000000:lib(0):func(0):reason(0), errno 10054
ErrorNr.:56

当我更改为 9mb 或更小的文件时,一切正常。

另外:从 cmd (Windows 7) 调用 CURL 时出现同样的错误:

curl --form upload=@myfile.jpg --form press=OK https://localhost:44303/api/v1/File -k -sslv3 

=> 一切都发生在我的本地机器上,所以没有防火墙也没有 vpn。

我尝试了多种参数组合,但没有任何进展。实际上,它比实际测试更多的是猜测。也许我一直忽略了一件小事?

提前非常感谢!

【问题讨论】:

    标签: c# php asp.net curl file-upload


    【解决方案1】:

    【讨论】:

    • 我尝试了建议的解决方案,但对我没有任何效果。请参阅我编辑的问题。
    【解决方案2】:

    在猜测了将近一天后,我有了将 asp.net 部分发布到 Azure 网站的想法,因为这可能是我本地配置的问题。

    当我发布它时,我不得不删除我的 php curl 配置的这一行:

    //curl_setopt($ch, CURLOPT_SSLVERSION, 3); 
    

    借助 Azure 网站,我现在可以上传更大的文件 - 就像它应该的那样。

    我试图理解为什么它可以在 Azure 上运行,但不能在 localhost 上运行 - 但实际上我还没有找到答案。看起来像是 IIS express 和 Visual Studio 2013 的一些配置问题。也许这对某人有帮助。

    【讨论】:

      【解决方案3】:

      这种仅上传 9MB 的限制可能是由于使用安全的 https URL 而不是非安全的 http URL。

      所以在 php 脚本中你应该将第 5 行更改为:

          $url = 'http://localhost:44307/api/v1/File';
      

      适当的 curl 命令是:

          curl --form upload=@myfile.jpg --form press=OK http://localhost:44303/api/v1/File -k -sslv3
      

      使用安全 URL 时,上传实际上会在不到十秒的时间内停止,这可以在 curl 进度表中看到。几分钟后连接关闭,假装实际问题出在服务器上(错误代码 56 表示连接已被对等方重置)。

      【讨论】:

        猜你喜欢
        • 2012-07-07
        • 1970-01-01
        • 1970-01-01
        • 2015-01-29
        • 1970-01-01
        • 2016-05-04
        • 2012-08-15
        • 2016-06-14
        • 2015-04-22
        相关资源
        最近更新 更多