2014-03-26 82 views
0

我有一个web api(再次)的问题。 我想将文件上传到我的S3存储,目前我做这个通过正常的控制器,它看起来是这样的:asp.net web api upload

public class _UploadController : BaseController 
{ 
    public JsonNetResult StartUpload(string id, HttpPostedFileBase file) 
    { 
     try 
     { 
      using (var service = new ObjectService(ConfigurationManager.AppSettings["AWSAccessKey"], ConfigurationManager.AppSettings["AWSSecretKey"], this.CompanyId)) 
      { 
       if (!service.Exists(file.FileName)) 
       { 
        service.Add(id); 

        var stream = new MemoryStream(); 
        var caller = new AsyncMethodCaller(service.Upload); 

        file.InputStream.CopyTo(stream); 

        var result = caller.BeginInvoke(id, stream, file.FileName, new AsyncCallback(CompleteUpload), caller); 
       } 
       else 
        throw new Exception("This file already exists. If you wish to replace the asset, please edit it."); 

       return new JsonNetResult { Data = new { success = true } }; 
      } 
     } catch(Exception ex) 
     { 
      return new JsonNetResult { Data = new { success = false, error = ex.Message } }; 
     } 
    } 

    public void CompleteUpload(IAsyncResult result) 
    { 
     using (var service = new ObjectService(ConfigurationManager.AppSettings["AWSAccessKey"], ConfigurationManager.AppSettings["AWSSecretKey"], this.CompanyId)) 
     { 
      var caller = (AsyncMethodCaller)result.AsyncState; 
      var id = caller.EndInvoke(result); 

      //this.service.Remove(id); 
     } 
    } 

    // 
    // GET: /_Upload/GetCurrentProgress 

    public JsonResult GetCurrentProgress(string id) 
    { 
     try 
     { 
      var bucketName = this.CompanyId; 
      this.ControllerContext.HttpContext.Response.AddHeader("cache-control", "no-cache"); 

      using (var service = new ObjectService(ConfigurationManager.AppSettings["AWSAccessKey"], ConfigurationManager.AppSettings["AWSSecretKey"], bucketName)) 
      { 
       return new JsonResult { Data = new { success = true, progress = service.GetStatus(id) } }; 
      } 
     } 
     catch (Exception ex) 
     { 
      return new JsonResult { Data = new { success = false, error = ex.Message } }; 
     } 
    } 
} 

这工作得很好,但我想创建一个网页API来处理上传。 网络api版本没有工作(Unsupported media type when uploading using web api

所以我开始看教程。我碰到这种方法:

public async Task<HttpResponseMessage> PostFile() 
{ 
    // Check if the request contains multipart/form-data. 
    if (!Request.Content.IsMimeMultipartContent()) 
    { 
     throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); 
    } 

    string root = HttpContext.Current.Server.MapPath("~/App_Data"); 
    var provider = new MultipartFormDataStreamProvider(root); 

    try 
    { 
     StringBuilder 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) 
     { 
      FileInfo fileInfo = new FileInfo(file.LocalFileName); 
      sb.Append(string.Format("Uploaded file: {0} ({1} bytes)\n", fileInfo.Name, fileInfo.Length)); 
     } 
     return new HttpResponseMessage() 
     { 
      Content = new StringContent(sb.ToString()) 
     }; 
    } 
    catch (System.Exception e) 
    { 
     return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); 
    } 
} 

这里的问题是,它通过创建一个MultipartFormDataStreamProvider和异步读取内容的文件保存到〜/ App_Data文件。 我想要做的是捕获数据并将其存储在内存流中,然后将其上传到s3。

这可能吗?我不想将我的文件上传到我的服务器,然后再上传到s3。

回答