1

我有用于将文件上传到 PHP Web 服务的 ac# 函数。PHP Web 服务期待以下内容

  • 一个名为 UploadFileRequestDto 的 POST 参数,其中包含一些 XML 数据
  • 文件流

由于某些奇怪的原因,$_POST 参数仅在某些时候包含 UploadFileRequestDto。如果我看一下内容

file_get_contents("php://input"))

我可以看到请求正在按预期通过 UploadFileRequestDto 包括在内。

做一个

print_r($_REQUEST)

正在返回一个空数组。

谁能帮我解决这个问题,我的C#函数如下规定

public string UploadFile(UploadFileRequestDto uploadFileRequestDto,string fileToUpload, string fileUploadEndpoint)
    {
        try
        {
            var request = (HttpWebRequest)WebRequest.Create(fileUploadEndpoint);
            request.ReadWriteTimeout = 1000 * 60 * 10;
            request.Timeout = 1000 * 60 * 10;
            request.KeepAlive = false;

            var boundary = "B0unD-Ary";

            request.ContentType = "multipart/form-data; boundary=" + boundary;
            request.Method = "POST";

            var postData = "--" + boundary + "\r\nContent-Disposition: form-data;";
            postData += "name=\"UploadFileRequestDto\"\r\n\r\n";
            postData += string.Format("{0}\r\n", SerializeUploadfileRequestDto(uploadFileRequestDto));
            postData += "--" + boundary + "\r\n";

            postData += "--" + boundary + "\r\nContent-Disposition: form-data;name=\"file\";filename=\"" + Path.GetFileName(fileToUpload) + "\"\r\n";
            postData += "Content-Type: multipart/form-data\r\n\r\n";

            var byteArray = Encoding.UTF8.GetBytes(postData);

            byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            byte[] filedata = null;
            using (var reader = new BinaryReader(File.OpenRead(fileToUpload)))
            {
                filedata = reader.ReadBytes((int)reader.BaseStream.Length);
            }

            request.ContentLength = byteArray.Length + filedata.Length + boundaryBytes.Length;
            request.GetRequestStream().Write(byteArray, 0, byteArray.Length);
            request.GetRequestStream().Write(filedata, 0, filedata.Length);
            request.GetRequestStream().Write(boundaryBytes, 0, boundaryBytes.Length);

            var response = request.GetResponse();
            var data = response.GetResponseStream();
            var sReader = new StreamReader(data);
            var sResponse = sReader.ReadToEnd();
            response.Close();

            return sResponse.TrimStart(new char[] { '\r', '\n' });
        }
        catch (Exception ex)
        {
            LogProvider.Error(string.Format("OzLib.Infrastructure : WebHelper : public string UploadFile(UploadFileRequestDto uploadFileRequestDto, string fileUploadEndpoint) : Exception = {0}", ex.ToString()));
        }
4

1 回答 1

1

好的,我发现了问题,

post_max_size

php.ini 中的设置设置为 8M,我尝试上传的一些文件超过了 8M。将此设置更改为 16M 并重新启动 PHP 服务。

当文件大小超过设置的限制时,$_POST 全局为空。

于 2013-05-30T11:02:47.810 回答