如何将图片上传到ServiceStack服务? [英] How do I upload an image to a ServiceStack service?
问题描述
我有一个图像的路径,并使用下面的代码发送给我的服务器;
$ b
I have the path of an image, and use the following code to send it to my server;
HttpWebRequest client = (HttpWebRequest)WebRequest.Create("http://212.175.132.168/service/api/upload/cab.jpg");
client.Method = WebRequestMethods.Http.Post;
// the following 4 rows enable streaming
client.AllowWriteStreamBuffering = false;
client.SendChunked = true;
client.ContentType = "multipart/form-data;";
client.Timeout = int.MaxValue;
using (FileStream fileStream = System.IO.File.OpenRead (filePath)) {
fileStream.Copy (client.GetRequestStream ());
}
var response = new StreamReader (client.GetResponse ().GetResponseStream ()).ReadToEnd ();
但是代码不起作用,图像没有附加。我在这里做错了什么?
But the code doesn't work, image isn't attached. What I am I doing wrong here?
推荐答案
HttpWebRequest + multipart / form-data $ c通过使用 HttpWebRequest
和 multipart / form-data 来限制
$ b $> / code>要求您指定内容的边界。如果你不明白的话,这是相当多的工作,很容易造成上传失败。然而这里的这个问题涵盖了如何去做。
HttpWebRequest + multipart/form-data
needs boundaries:
By using HttpWebRequest
with multipart/form-data
requires that you specify the boundaries of your content. Which is rather a lot of work and can easily cause corrupt uploads if you don't understand it. However this question here covers how to do it.
但是,当您使用ServiceStack后端时,最好的方法是使用 ServiceStack.Client PCL library 在你的Android应用程序中,它提供了易于使用的 JsonServiceClient
。 查看这个例子,了解一个带有ServiceStack演示的完整Android。
But as you are using a ServiceStack backend, then the best approach is to use the ServiceStack.Client PCL library in your Android application, which provides the easy to use JsonServiceClient
. See this example for a full Android with ServiceStack demonstration.
所以给一个简单的上传服务(在你的服务器端):
$ b
So given a simple Upload Service (on your server side):
[Route("/upload","POST")]
public class UploadFileRequest
{
// Example of other properties you can send with the request
public string[] Tags { get; set; }
}
class MyFileService : Service
{
public bool Post(UploadFileRequest request)
{
// Check a file has been attached
if(Request.Files == null || Request.Files.Length == 0)
throw new HttpError(400, "Bad Request", "No file has been uploaded");
// Save the file
Request.Files[0].SaveTo(Request.Files[0].FileName);
// Maybe store the tags (or any other data in the request)
// request.Tags
return true;
}
}
/ code>在你的Android应用程序,那么你只需要这样做:
$ b
Then with the JsonServiceClient
in your Android app, then your simply need to do this:
var filename = "cab.jpg"; // The path of the file to upload
var client = new JsonServiceClient("http://212.175.132.168/service/api/");
using(var fileStream = File.OpenRead(filename))
{
client.PostFileWithRequest<bool>(fileStream, "cab.jpg", new UploadFileRequest { Tags = new[] { "Cab", "Taxis", "NewYork", "Yellow" }});
}
我希望这有助于。
这篇关于如何将图片上传到ServiceStack服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
HttpWebRequest
和 multipart / form-data 来限制
$ b $> / code>要求您指定内容的边界。如果你不明白的话,这是相当多的工作,很容易造成上传失败。然而这里的这个问题涵盖了如何去做。
HttpWebRequest + multipart/form-data
needs boundaries:
By using HttpWebRequest
with multipart/form-data
requires that you specify the boundaries of your content. Which is rather a lot of work and can easily cause corrupt uploads if you don't understand it. However this question here covers how to do it.
但是,当您使用ServiceStack后端时,最好的方法是使用 ServiceStack.Client PCL library 在你的Android应用程序中,它提供了易于使用的 JsonServiceClient
。 查看这个例子,了解一个带有ServiceStack演示的完整Android。
But as you are using a ServiceStack backend, then the best approach is to use the ServiceStack.Client PCL library in your Android application, which provides the easy to use JsonServiceClient
. See this example for a full Android with ServiceStack demonstration.
所以给一个简单的上传服务(在你的服务器端):
$ b
So given a simple Upload Service (on your server side):
[Route("/upload","POST")]
public class UploadFileRequest
{
// Example of other properties you can send with the request
public string[] Tags { get; set; }
}
class MyFileService : Service
{
public bool Post(UploadFileRequest request)
{
// Check a file has been attached
if(Request.Files == null || Request.Files.Length == 0)
throw new HttpError(400, "Bad Request", "No file has been uploaded");
// Save the file
Request.Files[0].SaveTo(Request.Files[0].FileName);
// Maybe store the tags (or any other data in the request)
// request.Tags
return true;
}
}
/ code>在你的Android应用程序,那么你只需要这样做:
$ b
Then with the JsonServiceClient
in your Android app, then your simply need to do this:
var filename = "cab.jpg"; // The path of the file to upload
var client = new JsonServiceClient("http://212.175.132.168/service/api/");
using(var fileStream = File.OpenRead(filename))
{
client.PostFileWithRequest<bool>(fileStream, "cab.jpg", new UploadFileRequest { Tags = new[] { "Cab", "Taxis", "NewYork", "Yellow" }});
}
我希望这有助于。
这篇关于如何将图片上传到ServiceStack服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!