开发者社区> 问答> 正文

C上传文件不成功

c# 桌面开发上传文件不成功:代码如下

private string HttpPostData(string url, int timeOut, string fileKeyName,string filePath)
        {
            string responseContent;
            var memStream = new MemoryStream();
            var webRequest = (HttpWebRequest)WebRequest.Create(url);
            // 边界符
            var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
            // 边界符
            var beginBoundary = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");      //用ASCII编码方式也不成功
            var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            // 最后的结束符
            var endBoundary = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");      //用ASCII编码方式也不成功


            // 设置属性
            webRequest.Method = "POST";
            webRequest.Timeout = timeOut;
            webRequest.ContentType = " multipart/form-data; boundary=" + boundary;


            // 写入文件
            string filePartHeader = boundary + "\r\n" +
                "Content-Disposition:form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
                 "Content-Type:application/octet-stream\r\n\r\n";
            var header = string.Format(filePartHeader, fileKeyName, filePath);
            var headerbytes = Encoding.UTF8.GetBytes(header);                           //用ASCII编码方式也不成功


            memStream.Write(headerbytes, 0, headerbytes.Length);


            var buffer = new byte[1024];
            int bytesRead; // =0


            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                memStream.Write(buffer, 0, bytesRead);
            }


            // 写入结束边界符
            memStream.Write(endBoundary, 0, endBoundary.Length);


            webRequest.ContentLength = memStream.Length;


            var requestStream = webRequest.GetRequestStream();


            memStream.Position = 0;
            var tempBuffer = new byte[memStream.Length];
            memStream.Read(tempBuffer, 0, tempBuffer.Length);
            memStream.Close();


            requestStream.Write(tempBuffer, 0, tempBuffer.Length);
            requestStream.Close();


            var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();


            using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),Encoding.GetEncoding("utf-8")))
            {
                responseContent = httpStreamReader.ReadToEnd();
            }


            fileStream.Close();
            httpWebResponse.Close();
            webRequest.Abort();


            return responseContent;                                   //返回的结果:{"errcode":-1,"errmsg":"系统繁忙"}
        }


有C#上传文件的例子吗?谢谢了

展开
收起
michao 2015-09-25 12:52:57 6643 0
5 条回答
写回答
取消 提交回答
  • ReC上传文件不成功
    我开发了一个.net版本的Demo,可以去下载,里面有上传下载文件的例子, https://bbs.aliyun.com/read/311518.html?spm=0.0.0.0.hsEqRW

    2017-04-01 12:58:05
    赞同 展开评论 打赏
  • ReC上传文件不成功
    open.dingtalk.com/doc/index.html?spm=a3140.7785475.0.0.9RILzu
    请参考我们的文档
    2015-09-29 18:38:12
    赞同 展开评论 打赏
  • ReC上传文件不成功
    Java和PHP的Demo示例在哪里?
    2015-09-29 18:16:39
    赞同 展开评论 打赏
  • ReC上传文件不成功
    是不是目前不支持C#开发,必须得用JAVA或PHP之类的。C#这么多的开发人员,你们也得考虑一下嘛!
    2015-09-28 20:42:45
    赞同 展开评论 打赏
  • ReC上传文件不成功
    抱歉,目前没有C#上传文件的例子,您可以参考 Java或者PHP的demo

    -------------------------

    ReC上传文件不成功
    您好,非常抱歉,我们现在只有Java和PHP的Demo示例。
    建议可以参考一下示例代码,对比一下C#代码的请求参数设置。
    2015-09-25 16:18:13
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载