使用WebClient将图像上载到Web服务

时间:2013-04-15 11:05:23

标签: c# web-services webclient

使用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参数变为空,有人能解决问题吗?

1 个答案:

答案 0 :(得分:0)

您在哪里将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 += ...