Asp.net mvc 4 MultipartFormDataStreamProvider可上载mime文件和表单数据
由于Azure要求,使用RTM版本、框架4 代码在Azure emulator上运行时不会出错-我怀疑问题与path/environment变量有关。 在Azure web角色上失败,出现404错误 以下是控制器代码:Asp.net mvc 4 MultipartFormDataStreamProvider可上载mime文件和表单数据,asp.net-mvc-4,Asp.net Mvc 4,由于Azure要求,使用RTM版本、框架4 代码在Azure emulator上运行时不会出错-我怀疑问题与path/environment变量有关。 在Azure web角色上失败,出现404错误 以下是控制器代码: [ValidateInput(false)] public HttpResponseMessage Post() { try { if (!Request.Content.IsMimeMultipartContent("fo
[ValidateInput(false)]
public HttpResponseMessage Post()
{
try
{
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string tempPath = RoleEnvironment.GetLocalResource("tempStorage").RootPath;
Environment.SetEnvironmentVariable("TEMP", tempPath);
Environment.SetEnvironmentVariable("TMP", tempPath);
string Host = "test";
string StorageConnection = "credentials here";
string Product = "Product";
string CompanyID = "Company";
DocStorage docStorage = new DocStorage(Host, Product, CompanyID, StorageConnection);
var multipartStreamProvider = new AzureBlobStorageMultipartProvider(docStorage.BlobContainer, tempPath);
Stream reqStream = Request.Content.ReadAsStreamAsync().Result;
if (reqStream.CanSeek)
{
reqStream.Position = 0;
}
Request.Content.ReadAsMultipartAsync<AzureBlobStorageMultipartProvider>(multipartStreamProvider).ContinueWith<List<FileDetails>>(t =>
{
if (t.IsFaulted)
{
throw t.Exception;
}
AzureBlobStorageMultipartProvider provider = t.Result;
foreach (var fileData in provider.FileData)
{
string fileName = Path.GetFileName(fileData.Headers.ContentDisposition.FileName.Trim('"'));
string fileNameBlob = Path.GetFileName(fileData.LocalFileName.Trim('"'));
CloudBlob blob = docStorage.BlobContainer.GetBlobReference(fileNameBlob);
if (!string.IsNullOrEmpty(provider.FormData["company"]))
blob.Metadata[AriettDocStorage.FileNameFileLocation] = provider.FormData["company"];
blob.SetMetadata();
}
return provider.Files;
});
return new HttpResponseMessage(HttpStatusCode.OK);
}
catch
{
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
}
我解决了这个问题,它是一些小事情,不相关的路径,这是正确的 我希望你能详细说明解决办法。我有同样的404问题,使用博客上提供的代码。
public class AzureBlobStorageMultipartProvider : MultipartFormDataStreamProvider
{
public CloudBlobContainer Container;
public AzureBlobStorageMultipartProvider(CloudBlobContainer container, string tempPath)
: base(tempPath)
{
Container = container;
}
public override Task ExecutePostProcessingAsync()
{
// Upload the files to azure blob storage and remove them from local disk
foreach (var fileData in this.FileData)
{
string fileName = Path.GetFileName(fileData.Headers.ContentDisposition.FileName.Trim('"'));
// Retrieve reference to a blob
string fileNameBlob = Path.GetFileName(fileData.LocalFileName.Trim('"'));
CloudBlob blob = Container.GetBlobReference(fileNameBlob);
blob.Properties.ContentType = fileData.Headers.ContentType.MediaType;
blob.UploadFile(fileData.LocalFileName);
blob.SetProperties();
File.Delete(fileData.LocalFileName);
Files.Add(new FileDetails
{
ContentType = blob.Properties.ContentType,
Name = blob.Name,
Size = blob.Properties.Length,
Location = blob.Uri.AbsoluteUri
});
}
return base.ExecutePostProcessingAsync();
}