Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UploadData和UploadFile WebClient之间的差异_C#_Rest_Webclient - Fatal编程技术网

C# UploadData和UploadFile WebClient之间的差异

C# UploadData和UploadFile WebClient之间的差异,c#,rest,webclient,C#,Rest,Webclient,[基本上和这家伙一样,不幸的是线程在被应答之前就死了。] 我可以使用WebClient UploadFile上传文件,但无法使用UploadData上传转换为字节数组的相同文件 提琴差: 上载时数据:内容长度:13264 内容类型:多部分/表单数据;边界=--------------8d77fde3018f8e5 上载时文件:内容长度:13457 内容类型:多部分/表单数据;边界=--------------8D77FDCFCC453C 为什么会这样?目前,我拥有的文件存储在内存中,而不是文件中

[基本上和这家伙一样,不幸的是线程在被应答之前就死了。]

我可以使用WebClient UploadFile上传文件,但无法使用UploadData上传转换为字节数组的相同文件

提琴差: 上载时数据:内容长度:13264

内容类型:多部分/表单数据;边界=--------------8d77fde3018f8e5

上载时文件:内容长度:13457

内容类型:多部分/表单数据;边界=--------------8D77FDCFCC453C

为什么会这样?目前,我拥有的文件存储在内存中,而不是文件中,在本地保存文件实际上不是一个选项

有人有什么想法吗

编辑:

var bytes = System.IO.File.ReadAllBytes("dummy.pdf");


    using (WebClient client = new WebClient())
    {
        client.Headers.Add("Access-Token", token);
        client.Headers.Add("Client-Secret", clientSecret);

        client.UploadFile("website", "dummy.pdf");

    }
    string boundary = "---------------------" + DateTime.Now.Ticks.ToString("x");
    var newLine = Environment.NewLine;
    var fileHeaderFormat = "--"+boundary + newLine +
                           "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" + newLine +
        "Content-Type: application/octet-stream" + newLine + newLine;


    var req = (HttpWebRequest)HttpWebRequest.Create(@"website");
    req.Method = WebRequestMethods.Http.Post;
    req.ContentType = "multipart/form-data; boundary=" + boundary;
    req.Headers.Add("Access-Token", token);
    req.Headers.Add("Client-Secret", clientSecret);
    req.Headers.GetType().InvokeMember("ChangeInternal",
        BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
        Type.DefaultBinder, req.Headers, new object[] { "Connection", "Keep-Alive" }
    );


    using (var reqStream = req.GetRequestStream())
    {
        var reqWriter = new StreamWriter(reqStream);
        var tmp = string.Format(fileHeaderFormat, "file", "dummy.pdf");
        reqWriter.Write(tmp);
        reqWriter.Flush();
        reqStream.Write(bytes, 0, bytes.Length);
    }
    var res = req.GetResponse();
    using (var resStream = res.GetResponseStream())
    {
        var reader = new StreamReader(resStream);
        var t = reader.ReadToEnd();
    }

您能演示如何将文件转换为字节数组吗?var bytes=System.IO.file.ReadAllBytes(“dummy.pdf”);在另一个场景中,我得到一个字节数组作为对pdf生成器请求的响应。
但是我无法上传使用UploadData转换为字节数组的相同文件。
您是否看到任何错误,您看到的行为是什么?没有错误,唯一不同的是文件大小,UploadFile似乎在文件中添加了一些数据,而UploadFile没有。