红酒泡绿茶 2014-05-08 09:07
浏览 142
已采纳

将二进制数据从C#上传到PHP

I want to upload files from a Windows C# application to a web server running PHP.

I am aware of the WebClient.UploadFile method but I would like to be able to upload a file in chunks so that I can monitor progress and be able to pause/resume.

Therefore I am reading part of the file and using the WebClient.UploadData method.

The problem I am having is that I don't know how to access data sent with UploadData from php. If I do print_r on the post data I can see that there is binary data but I don't know the key to access it. How can I access the binary data? Is there a better way I should be doing this altogether?

String file = "C:\\Users\\Public\\uploadtest\\4.wmv";

using (BinaryReader b = new BinaryReader(File.Open(file, FileMode.Open)))
{
    int pos = 0;
    int required = 102400;
    b.BaseStream.Seek(pos, SeekOrigin.Begin);
    byte[] by = b.ReadBytes(required);

    using (WebClient wc = new WebClient()){
        wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        byte[] result = wc.UploadData("http://192.168.0.52/html/application.php", "POST", by);
        String s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);
        MessageBox.Show(s);
    }
}

enter image description here

  • 写回答

2条回答 默认 最新

  • duanbai1974 2014-05-08 10:28
    关注

    This is how I do my HTTP communications.

    I guess when I reach using(), the HTTP connection is being set up, and inside the using() {...} body you can do pausing and stuff.

    string valueString = "...";
    string uriString = "http://someUrl/somePath";
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uriString);
    httpWebRequest.Method = "POST";
    string postData = "key=" + Uri.EscapeDataString(valueString);
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    httpWebRequest.ContentType = "application/x-www-form-urlencoded";
    httpWebRequest.ContentLength = byteArray.Length;
    using (Stream dataStream = httpWebRequest.GetRequestStream())
    {
        // do pausing and stuff here by using a while loop and changing byteArray.Length into the desired length of your chunks
        dataStream.Write(byteArray, 0, byteArray.Length);
    }
    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    Stream receiveStream = httpWebResponse.GetResponseStream();
    StreamReader readStream = new StreamReader(receiveStream);
    string internalResponseString = readStream.ReadToEnd();
    

    But when uploading a file, you should probably use multipart/form-data in stead of application/x-www-form-urlencoded. See also: http://www.php.net/manual/en/features.file-upload.post-method.php

    In php you can use the superglobal variable $_FILES (eg print_r($_FILES); ) to access the uploaded file.

    And also read this: https://stackoverflow.com/a/20000831/1209443 for more information how to deal with multipart/form-data

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?