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。