【问题标题】:How can I pass parameters and uploading image at the same time to One web api?如何将参数和上传图像同时传递给 One web api?
【发布时间】: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&amp;password=pw,然后你应该使用HTTPS,因为你以明文形式传递密码(至少我认为)
  • 我看不懂,请给个样本?
  • 您的帖子可以使用参数Post(string email, string pw),然后您可以访问您体内的参数,使用http://yoursite.com/uploadphotos?email=whatever&amp;pw=b4df00d 之类的查询参数和图片附件发布到网址
  • so public Task Post( ) 应该是 Post(String email,String password) ?

标签: c# .net asp.net-mvc asp.net-web-api image-uploading


【解决方案1】:

什么对我有用:

public async Task<HttpResponseMessage> PostFile(string a, string b)
{
     var requestStream = await Request.Content.ReadAsStreamAsync();
  ...
}

路由:

config.Routes.MapHttpRoute(
    name: "ControllerApi",
    routeTemplate: "/{controller}/{a}/{b}"
);

发送文件:

var request = (HttpWebRequest) HttpWebRequest.Create("http://host/controller/hello/world");
request.Method = "POST";
var stream = request.GetRequestStream();
var docFile = File.OpenRead(sourceFile);
docFile.CopyTo(stream);
docFile.Close();
stream.Close();
var response = request.GetResponse();

【讨论】:

  • 什么是 var requestStream ?如何在 web api 端获取参数
  • 应该是 Task PostFile(...) 或者我可以是 Task Post(...)
  • 我试了一下,它给出了这个错误 404 not found (conn.getResponseMessage())
  • "hello" 将存储在字符串 a 中,而 "world" 将存储在字符串 b 中。您可以只使用 Post 或 PostXY。 requestStream 是文件的流。
  • 关于 404:您是否为主机/控制器设置了正确的值?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-20
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
  • 2019-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多