【发布时间】:2015-02-13 15:31:28
【问题描述】:
我使用这个控制器:
public class uploadphotosController : ApiController
{
public Task<HttpResponseMessage> Post( )
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root =HostingEnvironment.MapPath("~/photos");//Burdaki app data klasoru degisecek
var provider = new MultipartFormDataStreamProvider(root);
// Read the form data and return an async task.
var task = Request.Content.ReadAsMultipartAsync(provider).
ContinueWith<HttpResponseMessage>(t =>
{
if (t.IsFaulted || t.IsCanceled)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
}
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
string fileName = file.LocalFileName;
string originalName = file.Headers.ContentDisposition.FileName;
FileInfo file2 = new FileInfo(fileName);
file2.CopyTo(Path.Combine(root, originalName.TrimStart('"').TrimEnd('"')), true);
file2.Delete();
//Trace.WriteLine(file.Headers.ContentDisposition.FileName);
// Trace.WriteLine("Server file path: " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
});
return task;
}
}
我在 WebApiConfig.cs 中使用它
config.Routes.MapHttpRoute(
name: "DefaultApi_uploadphotos",
routeTemplate: "api/{ext}/uploadphotos/",
defaults: new
{
controller = "uploadphotos"
});
它工作正常,但我需要同时上传带有发送电子邮件和密码的图像。因为如果用户存在,我想上传图片。以我的方式,每个人都以用户身份上传照片。我想将图像和一些参数(如电子邮件和密码)发送到同一个 Web api。
我该怎么做?
提前致谢
【问题讨论】:
-
你应该能够使用查询参数
?email=email&password=pw,然后你应该使用HTTPS,因为你以明文形式传递密码(至少我认为) -
我看不懂,请给个样本?
-
您的帖子可以使用参数
Post(string email, string pw),然后您可以访问您体内的参数,使用http://yoursite.com/uploadphotos?email=whatever&pw=b4df00d之类的查询参数和图片附件发布到网址 -
so public Task
Post( ) 应该是 Post(String email,String password) ?
标签: c# .net asp.net-mvc asp.net-web-api image-uploading