【发布时间】:2018-04-03 19:10:51
【问题描述】:
仍在寻找解决方案。任何人都知道如何做到这一点
我正在将图片从手机上传到服务。图片上传成功但不知道怎么保存
我正在使用compile 'com.squareup.retrofit2:retrofit:2.1.0'
改造方法
@Multipart
@POST("RestService/json/PostKYCDocImg/")
Call<UploadPictureResponse> getURLKYCDocImg(@Part MultipartBody.Part imageData);
在android上传的文件中
File file = new File(new URI(de.getAttachFilePath()).getPath());
RequestBody mFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part image = MultipartBody.Part.createFormData("image", file.getName(), mFile);
Call<UploadPictureResponse> call = apiService.getURLKYCDocImg(image);
在 Wcf 网络服务中,我收到的是
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/json/PostKYCDocImg/", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
[FaultContract(typeof(ServiceData))]
string UploadPicture(Stream imageData);
public string UploadPicture(Stream imageData)
{
//saveImgInSpecPath(fileFullPath, GetBytesFromStream(imageData,
//System.Text.Encoding.UTF8), imageData);
saveImgInSpecPath2(fileFullPath, imageData);
}
private static Boolean saveImgInSpecPath2(string fileFullPath, Stream imageData)
{
try
{
//Save image here which is in imageData as stream and return saved status
//var fileStream = File.Create(fileFullPath);
//imageData.CopyTo(fileStream);
//StreamReader sr = new StreamReader(imageData.InputStream);
//var fileStream = File.Create(fileFullPath);
//imageData.InputStream.Seek(0, SeekOrigin.Begin);
//imageData.InputStream.CopyTo(fileStream);
//here i want to save image file which imageData in the form of stream
bool exists = File.Exists(fileFullPath);
return exists;
}
catch (Exception ex)
{
return false;
}
}
我在“saveImgInSpecPath2”方法中尝试了很多代码,所有代码都成功上传到服务路径但保存错误
请建议从 android 保存在 wcf webservice 中的正确方法 改造
【问题讨论】:
-
but when i open image。完全不清楚您尝试在网络服务器上打开图像的位置、方式和方式。你告诉 Windows 照片查看器什么? -
出于测试目的,我在我的系统中托管了 Web 服务,当我转到保存的图像路径时,我无法在打开时看到该图像
-
byte[] 是否有写入流的 CopyTo 过载?您将流和流的字节一起发送到您的方法中。如果是您调用 CopyTo 方法的流,则流已耗尽,将复制零字节
-
好吧,准确地告诉你那条路是什么。如果文件在那里,您是否检查过资源管理器?它的大小是多少?和原版一样吗?
-
这是实际代码吗?
标签: c# android web-services wcf retrofit2