简体   繁体   English

上载文件Web服务,无法打开它

[英]Uploading file webservice, unable to open it

I'm writting an android app which has to send a picture on a wcf webservice. 我正在编写一个必须在wcf网络服务上发送图片的android应用。 My app can contact the web service and give it the picture. 我的应用可以联系网络服务并为其提供图片。 But, size are differents and I can't open the picture on the web service. 但是,大小是不同的,我无法在Web服务上打开图片。

EDIT :By changing the webservice part I got the exact same size for both. 编辑:通过更改webservice部分,我得到了完全相同的大小。 But, still unable to open it. 但是,仍然无法打开它。

Android part (file size 20ko) : 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());
}

Edit : Webservice (file size 20ko): 编辑: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();


}

You read the file in chunks but write only the last chunk: 您分块读取文件,但仅写入最后一个块:

// following line is called once, should be called after each read
fileToupload.Write(bytearray, 0, bytearray.Length); 

So try like this: 因此,尝试这样:

/*...*/
do
{
    bytesRead = image.Read(bytearray, 0, bytearray.Length);
    totalBytesRead += bytesRead;
    fileToupload.Write(bytearray, 0, bytesRead);
} while (bytesRead > 0);

fileToupload.Close();
fileToupload.Dispose();
/*...*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM