我想创建一个WEB API,将给定客户端的文件上传到Azure。为此,我知道我可以使用类:MultipartFormDataStreamProvider(不确定此类是否适用于Azure)
我希望可以从各种应用程序访问此API。对于初学者来说,一个简单的.NET应用但我的问题是,我可以使用相同的API来处理来自Android应用程序,Windows 8应用程序等的文件上传吗?
如果无法做到这一点,那么所有这些应用程序是否需要公开单独的API?
我对API的想法是,它可以在各种应用程序中用于所需的功能。但在这种情况下,上传文件所需的类将限制其使用。
答案 0 :(得分:3)
@nikhil pinto:ASP.NET Web API可帮助您创建可由任何支持HTTP请求的客户端使用的REST-Web服务。我在这里找到了这篇文章
http://hintdesk.com/android-upload-files-to-asp-net-web-api-service
非常好,因为它说明了我们如何从不同的客户端调用Web服务。看看吧。
答案 1 :(得分:1)
上传图片的网络Api代码
[System.Web.Http.AcceptVerbs("POST")]
public async Task<object> AddAttachment()
{
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
}
NamedMultipartFormDataStreamProvider streamProvider = new NamedMultipartFormDataStreamProvider(
HttpContext.Current.Server.MapPath("~/App_Data/"));
await Request.Content.ReadAsMultipartAsync(streamProvider);
string path = streamProvider.FileData.Select(entry => entry.LocalFileName).First();
byte[] imgdata = System.IO.File.ReadAllBytes(path);
return new
{
FileNames = streamProvider.FileData.Select(entry => entry.LocalFileName),
};
}
答案 2 :(得分:1)
请找到以下示例代码,以通过post方法将多个文件发送到.Net Web api
.Net Web API方法
[HttpPost]
public string SaveImage()
{
string uploadedfilelist="";
if (((IList)HttpContext.Current.Request.Form.AllKeys).Contains("fileModel"))
{
var activity = JsonConvert.DeserializeObject<SocialCompanyPost>(HttpContext.Current.Request.Form["fileModel"]);
var files = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files : null;
if (files != null && files.Count > 0)
{
for (int i = 0; i < files.Count; i++)
{
if (files[i].ContentLength > 0)
{
try
{
var filepath = HttpContext.Current.Server.MapPath("~/Images/ServiceCenters/" + files[i].FileName);//, ms.ToArray());
files[i].SaveAs(filepath);
uploadedfilelist += files[i].FileName+",";
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
}
}
return uploadedfilelist;
}
客户端MVC应用程序post方法调用Web api post方法
public string UploadImages(ServiceCenterPost socialCompany)
{
var url = "api/Images/SaveImage";
if (socialCompany.ThumbnailUrlFileUpload.Count() > 0)
{
var fullURL = ConfigurationManager.AppSettings["ServiceApi"] + url;
HttpClient client = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
var stringContent = new StringContent(JsonConvert.SerializeObject(new SocialCompanyAdmin { CompanyCode = socialCompany.CompanyCode }));
stringContent.Headers.Add("Content-Disposition", "form-data; name=\"fileModel\"");
form.Add(stringContent, "json");
//loop each files from file upload control
foreach (var file in socialCompany.ThumbnailUrlFileUpload)
{
if (file != null && file.ContentLength > 0)
{
var streamContent = new StreamContent(file.InputStream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
streamContent.Headers.Add("Content-Disposition", "form-data; name=\"file\"; filename=\"" + Path.GetFileName(file.FileName) + "\"");
form.Add(streamContent, "file", Path.GetFileName(file.FileName));
}
}
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
client.DefaultRequestHeaders.Add("Accept-Language", Language);
var response = client.PostAsync(fullURL, form).Result;
if (response.IsSuccessStatusCode)
{
string modelObject = response.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<string>(modelObject);
}
}
return null;
}