问题描述
我有一个ASP.Net的Web API应用程序,允许客户端(HTML页面和iPhone应用程序)上传图片到。我使用的是异步上传的任务,因为在这个<一个描述href=\"http://www.$c$cguru.com/csharp/.net/uploading-files-asynchronously-using-asp.net-web-api.htm\">article.
I have an ASP.Net Web API application that allows clients (html pages and iPhone apps) to upload images to. I am using an async upload task as described in this article.
当我想保存到文件系统,因为这是这个code自动完成,似乎幕后一切的伟大工程。但是,我不希望上传的文件保存到文件系统中。相反,我想借此上传流,并通过它传递给使用AWS SDK用于.NET的Amazon S3的桶中。
Everything works great when I want to save to the file system because that's what this code does automatically, behind the scenes it seems. But, I don't want to save the uploaded files to the file system. Instead, I want to take the uploaded stream and pass it through to an Amazon S3 bucket using the AWS SDK for .Net.
我有code设置发送流高达AWS。我想不通的问题是如何得到而不是它会自动保存到磁盘的从Web API方法上传的内容流。
I have the code set up to send the stream up to AWS. The problem I can't figure out is how to get the uploaded content stream from the Web API method instead of having it automatically save to disk.
我希望会有我可以覆盖的 MultipartFormDataStreamProvider 的这样可以让我做别的事情比保存到磁盘其他上传的内容虚方法,但似乎并没有被
I was hoping there would be a virtual method I could override in MultipartFormDataStreamProvider which would allow me to do something else with the uploaded content other than save to disk, but there doesn't seem to be.
有什么建议?
推荐答案
您可以覆盖MultipartFormDataStreamProvider的GetStream方法返回一个流而不是一个文件流,但您的AWS流,但也存在一些问题,这样做(这我不会细说了)。相反,你可以创建一个供应商从抽象基类派生MultipartStreamProvider。下面的示例主要基于MultipartFormDataStreamProvider和MultipartFileStreamProvider的实际来源$ C $ C。您可以检查 <一个href=\"http://aspnetwebstack.$c$cplex.com/SourceControl/changeset/view/b4631c0ef899fdccf210cda4c0e39591e67537b7#src/System.Net.Http.Formatting/MultipartFormDataStreamProvider.cs\">here和 更多细节。下面的示例:
You could override MultipartFormDataStreamProvider's GetStream method to return a stream which is not a file stream but your AWS stream, but there are some issues doing so(which I will not elaborate here). Instead you could create a provider deriving from the abstract base class MultipartStreamProvider. Following sample is heavily based on the actual source code of MultipartFormDataStreamProvider and MultipartFileStreamProvider. You can check here and here for more details. Sample below:
public class CustomMultipartFormDataStreamProvider : MultipartStreamProvider
{
private NameValueCollection _formData = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
private Collection<bool> _isFormData = new Collection<bool>();
private Collection<MyMultipartFileData> _fileData = new Collection<MyMultipartFileData>();
public NameValueCollection FormData
{
get { return _formData; }
}
public Collection<MultipartFileData> FileData
{
get { return _fileData; }
}
public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
// For form data, Content-Disposition header is a requirement
ContentDispositionHeaderValue contentDisposition = headers.ContentDisposition;
if (contentDisposition != null)
{
// If we have a file name then write contents out to AWS stream. Otherwise just write to MemoryStream
if (!String.IsNullOrEmpty(contentDisposition.FileName))
{
// We won't post process files as form data
_isFormData.Add(false);
MyMultipartFileData fileData = new MyMultipartFileData(headers, your-aws-filelocation-url-maybe);
_fileData.Add(fileData);
return myAWSStream;//**return you AWS stream here**
}
// We will post process this as form data
_isFormData.Add(true);
// If no filename parameter was found in the Content-Disposition header then return a memory stream.
return new MemoryStream();
}
throw new InvalidOperationException("Did not find required 'Content-Disposition' header field in MIME multipart body part..");
}
/// <summary>
/// Read the non-file contents as form data.
/// </summary>
/// <returns></returns>
public override async Task ExecutePostProcessingAsync()
{
// Find instances of HttpContent for which we created a memory stream and read them asynchronously
// to get the string content and then add that as form data
for (int index = 0; index < Contents.Count; index++)
{
if (_isFormData[index])
{
HttpContent formContent = Contents[index];
// Extract name from Content-Disposition header. We know from earlier that the header is present.
ContentDispositionHeaderValue contentDisposition = formContent.Headers.ContentDisposition;
string formFieldName = UnquoteToken(contentDisposition.Name) ?? String.Empty;
// Read the contents as string data and add to form data
string formFieldValue = await formContent.ReadAsStringAsync();
FormData.Add(formFieldName, formFieldValue);
}
}
}
/// <summary>
/// Remove bounding quotes on a token if present
/// </summary>
/// <param name="token">Token to unquote.</param>
/// <returns>Unquoted token.</returns>
private static string UnquoteToken(string token)
{
if (String.IsNullOrWhiteSpace(token))
{
return token;
}
if (token.StartsWith("\"", StringComparison.Ordinal) && token.EndsWith("\"", StringComparison.Ordinal) && token.Length > 1)
{
return token.Substring(1, token.Length - 2);
}
return token;
}
}
public class MyMultipartFileData
{
public MultipartFileData(HttpContentHeaders headers, string awsFileUrl)
{
Headers = headers;
AwsFileUrl = awsFileUrl;
}
public HttpContentHeaders Headers { get; private set; }
public string AwsFileUrl { get; private set; }
}
这篇关于是否有可能重写MultipartFormDataStreamProvider这样就不会上传保存到文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!