使用.net 4.5创建web api任务

时间:2013-02-09 12:06:08

标签: c# asp.net asp.net-web-api

我有一个在我的webapi项目中上传图像的Task方法。一切正常但我想在单独的任务/线程中上传到我的AWS云流程,然后在该流程完成后运行一些功能。总而言之,这就是我想要完成的事情。

  1. http请求带有照片或视频的API
  2. 完成了一些验证工作,然后启动了启动云上传的新线程
  3. 以200或某种类型的OK状态结束第一个请求

  4. 当第一个请求结束时,正在进行上传,完成后,在其上运行某种完整的功能。客户端不需要使用此功能进行更新,但需要保存一些数据。

  5. 感谢您的帮助

        public async Task<HttpResponseMessage> PostMedia([FromUri]string userId, [FromUri]string eventId,[FromUri]string city,
                [FromUri]string title)
            {
                // Check if the request contains multipart/form-data. 
                if (!Request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }
                //temp directory
                string root = HttpContext.Current.Server.MapPath("~/App_Data");
                var provider = new MultipartFormDataStreamProvider(root);
                try
                {
                    var sb = new StringBuilder(); // Holds the response body 
    
                    // Read the form data and return an async task. 
                    await Request.Content.ReadAsMultipartAsync(provider);
    
                    // This illustrates how to get the form data. 
                    foreach (var key in provider.FormData.AllKeys)
                    {
                        foreach (var val in provider.FormData.GetValues(key))
                        {
                            sb.Append(string.Format("{0}: {1}\n", key, val));
                        }
                    }
    
                    // This illustrates how to get the file names for uploaded files. 
                    foreach (var file in provider.FileData)
                    {
    //Needs to kick off new task/thread
                        using (
                   var client = AWSClientFactory.CreateAmazonS3Client("key",
                                                                           "key"))
                        {
                            var fileInfo = new FileInfo(file.LocalFileName);
                            using (var stream = fileInfo.Open(FileMode.Open))
                            {
                                var request = new PutObjectRequest();
                                request.WithBucketName("Images")
                                       .WithCannedACL(S3CannedACL.PublicRead)
                                       .WithKey(ConfigurationManager.AppSettings["ImageBucket"] + fileInfo.Name +
                                       file.Headers.ContentDisposition.FileName.Substring(file.Headers.ContentDisposition.FileName.LastIndexOf('.')).Replace("\"", ""))
                                       .InputStream = stream;
                                client.PutObject(request);
    
                                sb.Append(string.Format("Uploaded file: {0} ({1} bytes)\n", fileInfo.Name, fileInfo.Length));
                                var mtype = MediaType.Picture;
                                  if(file.Headers.ContentType.MediaType.StartsWith("image"))
                                  {
                                      mtype = MediaType.Picture;
                                  }
                                  else if(file.Headers.ContentType.MediaType.StartsWith("video"))
                                  {
                                      mtype = MediaType.Video;
                                  }
    
                                  if (File.Exists(fileInfo.FullName))
                                  {
                                      File.Delete(fileInfo.FullName);
                                  }
                            }
                        }
                    }
                    return new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.Created,
                        Content = new StringContent(sb.ToString())
                    };
                }
                catch (System.Exception e)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
                }
            }
    

0 个答案:

没有答案