我使用Multiparti通过Retrofit使用Android发送文件,但是在我的服务器上我使用.Net C#构建Restful Service,然后如何创建restful以从Retrofit / Android接收文件multiparti?
样品:
[RoutePrefix("rest/files")]
public class ReceiveImagesController : ApiController
{
[AcceptVerbs("POST")]
[Route("SendFiles")]
public string sendFiles()
{
string retorno = "";
string path = "C:/temp";
// byte[] Bytes = new byte[files.Inpu]
return retorno;
}
}
答案 0 :(得分:1)
我的示例代码和我在webapi 2中使用文件上传。我认为您的问题将解决以下代码。
sing System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
namespace WebMvcTest.Controllers
{
[System.Web.Http.RoutePrefix("api/test")]
public class FileUploadController : ApiController
{
[System.Web.Http.Route("files")]
[System.Web.Http.HttpPost]
[ValidateMimeMultipartContentFilter]
public async Task<FileResult> UploadSingleFile()
{
var streamProvider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(streamProvider);
string descriptionResult = string.Empty;
var description =
streamProvider.Contents.AsEnumerable()
.FirstOrDefault(T => T.Headers.ContentDisposition.Name == "\"description\"");
if (description != null)
{
descriptionResult = await description.ReadAsStringAsync();
}
return new FileResult
{
FileNames = streamProvider.Contents.AsEnumerable().Select(T => T.Headers.ContentDisposition.FileName).ToArray(),
Names = streamProvider.Contents.AsEnumerable().Select(T => T.Headers.ContentDisposition.FileName).ToArray(),
ContentTypes = streamProvider.Contents.AsEnumerable().Where(T => T.Headers.ContentType != null).Select(T => T.Headers.ContentType.MediaType).ToArray(),
Description = descriptionResult,
CreatedTimestamp = DateTime.UtcNow,
UpdatedTimestamp = DateTime.UtcNow,
};
}
}
}