上传文件webservice,无法打开它
问题描述:
我正在写一个android应用程序,它必须在wcf webservice上发送图片。 我的应用程序可以联系Web服务并为其提供图片。 但是,大小是differents,我无法打开Web服务上的图片。上传文件webservice,无法打开它
编辑:通过改变webservice部分我得到了两个完全相同的大小。但是,仍然无法打开它。
Android部分(文件大小20ko):
File img;
try {
Log.i("image", "get file");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
Log.i("call", "end build");
MultipartEntity entity = new MultipartEntity();
entity.addPart("data", new FileBody(f));
httppost.setEntity(entity);
Log.i("call", "call");
HttpResponse response = httpclient.execute(httppost);
Log.i("call", "After");
}
catch (Exception e) {
Log.i("error cal image", e.toString());
}
编辑: web服务(文件大小20ko):
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "picture")]
public void UploadPicture(Stream image)
{
var ms = new MemoryStream();
image.CopyTo(ms);
var streamBytes = ms.ToArray();
FileStream f = new FileStream("C:\\appicture.jpg", FileMode.OpenOrCreate);
f.Write(streamBytes, 0, streamBytes.Length);
f.Close();
ms.Close();
image.Close();
}
答
你读以块的文件,但只写了最后一块:
// following line is called once, should be called after each read
fileToupload.Write(bytearray, 0, bytearray.Length);
所以试试这样:
/*...*/
do
{
bytesRead = image.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
fileToupload.Write(bytearray, 0, bytesRead);
} while (bytesRead > 0);
fileToupload.Close();
fileToupload.Dispose();
/*...*/
+0
Thx。但是,同样的问题。 – David
byte [] bytearray = new byte [10000];那是对的吗? – fonini
@fonini不,它不是,Thx。我改变了网络服务部分,但仍然无法打开图片。 – David