2

我知道这个问题已经在 stackoverflow 及其整个互联网上被问了很多,但似乎从来没有一个通用或最佳的解决方案可以将图像从多平台上传到 .NET REST 服务。

我从之前针对服务端提出的问题中找到了这个解决方案我的第一个问题是,将图像从 Android 上传到链接中指定的特定服务的最佳和最优化的方法是什么?

我的第二个问题是,如何添加带有数据的 JSON 来伴随正在上传的图像?我已经看到在标头参数中附加数据而不是作为 JSON 的解决方案?什么是完美的方法?

4

3 回答 3

1

关于你的第一个问题:

从 android 上传图片的“最佳”方式很大程度上取决于您的情况,例如:

  • 如果您正在处理敏感照片,“最好”的方式可能是通过 SSL 上传以更安全
  • 上传多张照片?也许一个聚合的压缩-上传-解压方法就足够了。

基本上我要说的是,针对您的特定需求使用最明显的方法。

关于问题2:

看看这个问题。

您可以使用 getBase64Image 方法在客户端获取图像字节,然后将其弹出到您发送到服务器的 json 中

于 2013-05-06T04:24:51.873 回答
1

I have few proposals while uploading file to web service:

  1. Write a web serivice that allow multi part file upload using MultipartFormDataStreamProvider
  2. Dont read the whole file content while uploading to web service. Rather use HttpClient library to upload mutiple part content. See MultipartEntity.

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("Service URL");
    FileBody body1 = new FileBody(new File(path), mimeType);
    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("part0", body1);
    httppost.setEntity(reqEntity);      
    
  3. Use android background service when uploading file. If it is big file then it will take long time to upload. If the app went to backgournd then upload process might be internupted.

  4. In the server side you need a custom implementation of MultipartFileStreamProvider class to pass additional file attributes.
于 2013-05-14T09:08:30.187 回答
0

下面是使用 wcf rest 上传文件的示例:

[ServiceContract]
interface IUploader
{
[WebInvoke(UriTemplate = "FileUpload?public_id={public_id}&tags={tags}",
  Method = "POST",
  ResponseFormat = WebMessageFormat.Json)]
  UploadImageResult UploadImage(Stream fileContents, string public_id, string tags);
}

public class Uploader : IUploader
{
public UploadImageResult UploadImage(Stream fileContents, string public_id, string tags)
{
try
{
byte[] buffer = new byte[32768];
FileStream fs = File.Create(Path.Combine(rootFolderPath, @"test\test.jpg"));
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = fileContents.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
fs.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);

fs.Close();
}
catch(Exception ex)
{
...
}
}
}

使用 c# 调用

// Create the REST URL. 
string requestUrl = @"http://localhost:2949/fileupload?public_id=id&tags=tags";
//file to upload make sure it exist
string filename = @"C:\temp\ImageUploaderService\vega.jpg";

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
request.Method = "POST";
//request.ContentType = "text/plain";
request.ContentType = "application/json; charset=utf-8";
byte[] fileToSend = File.ReadAllBytes(filename);
request.ContentLength = fileToSend.Length;
using (Stream requestStream = request.GetRequestStream())
 {
 // Send the file as body request. 
 requestStream.Write(fileToSend, 0, fileToSend.Length);
 requestStream.Close();
}

 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
 {
    Console.WriteLine("HTTP/{0} {1} {2}", response.ProtocolVersion,     (int)response.StatusCode, response.StatusDescription);
    string text;
     using (var sr = new StreamReader(response.GetResponseStream()))
                    {
                        text = sr.ReadToEnd();
                    }

                    dynamic testObj = Newtonsoft.Json.JsonConvert.DeserializeObject(text);

                    Console.WriteLine(string.Format("response url is {0}", testObj.url));
                }

问题 #2,您可以像示例中一样在请求的 url 上使用键/值对附加数据。

希望这会有所帮助。

于 2013-05-14T09:04:48.750 回答