Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将发送到wcf web服务的流另存为图像_C#_Android_Web Services_Wcf_Retrofit2 - Fatal编程技术网

C# 如何将发送到wcf web服务的流另存为图像

C# 如何将发送到wcf web服务的流另存为图像,c#,android,web-services,wcf,retrofit2,C#,Android,Web Services,Wcf,Retrofit2,我正在使用改型将图像文件发送到wcf web服务,在wcf web服务的保存端,我无法保存流文件 在安卓系统中,我喜欢 //ApiInterface.class @Multipart @POST("RestService/json/PostUploadFile/") Call<UploadFileResponse> uploadFile(@Part MultipartBody.Part file); 呼叫服务方法 private void callUploadFile(Reque

我正在使用改型将图像文件发送到wcf web服务,在wcf web服务的保存端,我无法保存流文件

在安卓系统中,我喜欢

//ApiInterface.class
@Multipart
@POST("RestService/json/PostUploadFile/")
Call<UploadFileResponse> uploadFile(@Part MultipartBody.Part file); 
呼叫服务方法

private void callUploadFile(RequestBody body, String fileName,
                           MainInteractor.OnFinishedListener listenerP) {
        final MainInteractor.OnFinishedListener listener = listenerP;

        HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("SessionID", "");
        headerMap.put("UserName", "");
        headerMap.put("FileName", fileName);
        OkHttpClient httpClient = ConnectToService.newInstance().addHeaders(getContext(), headerMap);

        ApiInterface apiService =
                ConnectToService.newInstance()
                        .getClient(httpClient).create(ApiInterface.class);

        Call<UploadFileResponse> call = apiService.uploadFile(body);
        call.enqueue(new Callback<UploadFileResponse>() {
            @Override
            public void onResponse(Call<UploadFileResponse> call, Response<UploadFileResponse> response) {
                if (response != null && response.body() != null) {
                    onFinished(response.body().getResult());
                } else {
                    if (response.message() != null) {
                        onFinishedFailure(response.message());
                    }
                }
            }

            @Override
            public void onFailure(Call<UploadFileResponse> call, Throwable t) {
                if (t.getLocalizedMessage() != null) {
                    onFinishedFailure(t.getLocalizedMessage());
                }
            }
        });
    }
在api调用中

@POST("RestService/json/PostUploadFile/")
    Call<UploadFileResponse> uploadFile(@Body RequestBody bytes);
@POST(“RestService/json/postploadfile/”)
调用uploadFile(@Body RequestBody字节);

您的流似乎是由表单数据提交的,这意味着流包含一些不必要的数据,例如提交的表单数据中的其他数据。需要注意的是,WCF默认不支持表单数据,我们通常使用第三方库,MultipartParser
这是下载页面。

在这种情况下,请使用以下代码段保存图像

public async Task UploadStream(Stream stream)
        {
            //the third-party library.
            MultipartParser parser = new MultipartParser(stream);

            if (parser.Success)
            {
                //absolute filename, extension included.
                var filename = parser.Filename;
                var filetype = parser.ContentType;
                var ext = Path.GetExtension(filename);
                using (var file = File.Create(Path.Combine(HostingEnvironment.MapPath("~/Uploads"), Guid.NewGuid().ToString() +ext)))
                {
                    await file.WriteAsync(parser.FileContents, 0, parser.FileContents.Length);
                }
            }
}

如果流是完整的二进制文件,请考虑下面的代码(我们使用HTTP报头来保存文件扩展名,因为WCF不允许在方法签名中包含另一个参数)。p>


如果问题仍然存在,请随时通知我。

图像似乎是base64encoded@TheGeneral你能告诉代码如何保存吗,真的我不知道如何保存,我搜索了很多,但没有任何效果…我已经尝试了MultipartParser,它只会导致无效参数异常,所以更改为“application/octet stream”现在它成功了。谢谢你的帮助谢谢分享你的解决方案,代码片段很好。
public string uploadFile(Stream imageData)
        {
            string fileName = WebOperationContext.Current.IncomingRequest.Headers.Get("fileName");
            string fileFullPath = "D:\\Share\\srinidhi\\Temp_" + fileName + ".Jpeg";

            Image img = System.Drawing.Image.FromStream(imageData);
            img.Save(fileFullPath, ImageFormat.Jpeg);

            return "success";
        }
@POST("RestService/json/PostUploadFile/")
    Call<UploadFileResponse> uploadFile(@Body RequestBody bytes);
public async Task UploadStream(Stream stream)
        {
            //the third-party library.
            MultipartParser parser = new MultipartParser(stream);

            if (parser.Success)
            {
                //absolute filename, extension included.
                var filename = parser.Filename;
                var filetype = parser.ContentType;
                var ext = Path.GetExtension(filename);
                using (var file = File.Create(Path.Combine(HostingEnvironment.MapPath("~/Uploads"), Guid.NewGuid().ToString() +ext)))
                {
                    await file.WriteAsync(parser.FileContents, 0, parser.FileContents.Length);
                }
            }
}
public async Task UploadStream(Stream stream)
        {
            var context = WebOperationContext.Current;
            string filename = context.IncomingRequest.Headers["filename"].ToString();
            string ext = Path.GetExtension(filename);
            using (stream)
            {
                //save the image under the Uploads folder on the server-side(root directory).
                using (var file = File.Create(Path.Combine(HostingEnvironment.MapPath("~/Uploads"), Guid.NewGuid().ToString() + ext)))
                {
                    await stream.CopyToAsync(file);
                }
            }
        }