【问题标题】:uploading image to the web service using WebClient使用 WebClient 将图像上传到 Web 服务
【发布时间】:2013-04-07 11:21:31
【问题描述】:

使用web client 将图像上传到网络服务时遇到问题, 我的代码是--->

Uri uri = new Uri("http://localhost/Test/SaveImage");
            string imageData = Convert.ToBase64String(data);
            WebClient web = new WebClient();
            web.UploadStringAsync(uri, "Post", imageData);
            web.UploadStringCompleted += new UploadStringCompletedEventHandler(web_UploadStringCompleted);

在上面的代码中,图片转换成字节数组,然后到Base64String上传。

但是在接收端--->

[HttpPost]
public bool SaveImage(string ImageBytes)   <---ImageBytes is Null
        {
                 ///// some code
        }

ImageBytes 参数变空,谁能找出问题所在?

【问题讨论】:

  • 它是一个字符串,不应该是字节数组,还是做一个64位的字符串转换,也许行得通。但是字节数组是我的首选。
  • @marko :: 我已经将它转换成 Base64String,你说什么?在接收端或其他地方???
  • 如果你做了UploadStringAsync(uri, "Post", "ImageData=" + imageData)呢?
  • 不,我现在还不确定。

标签: c# web-services webclient


【解决方案1】:

你曾在哪里将dataImageBytes 的值联系起来?

作为替代方案,您是否尝试过使用UploadValues?参考是here

在你的情况下,它会是这样的:

var uri = new Uri("http://localhost/Test/SaveImage");
var nameValueCollection = new NameValueCollection();
nameValueCollection.Add("ImageBytes", Convert.ToBase64String(data));
WebClient web = new WebClient();
web.UploadValuesAsync(uri, "Post", nameValueCollection);
web.OnUploadValuesCompleted += ...

【讨论】:

  • thr 不是 UploadValuesAsync 的扩展方法,我在我的 wp7 应用程序中使用它
  • 这很关键(使用 wp7),请在您的问题中说明
猜你喜欢
  • 2018-09-27
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
相关资源
最近更新 更多