我遵循此tutorial从Azure Blob存储上传/下载blob。
在我实施承载令牌认证(OAuth)
之前,该代码完美运行我从邮递员上传任何文件时遇到错误。以下是错误说明。
{ “消息”:“发生了错误。详细信息:索引超出范围。必须是非负数且小于 集合。\ r \ nParameter name:index“}
但是,文件已成功上传到我的Blob帐户中。但是,我仍然不断出现错误。
我附上了调试时获得的错误详细信息的图像。
我在上传控制器中提示错误的区域
try
{
await Request.Content.ReadAsMultipartAsync(provider);
}
catch (Exception ex)
{
return BadRequest($"An error has occured. Details: {ex.Message}");
}
我的AzureStorageMultipartFormDataStreamProvider类继承自MultipartFormDataStreamProvider
public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
if (parent == null) throw new ArgumentNullException(nameof(parent));
if (headers == null) throw new ArgumentNullException(nameof(headers));
// Generate a new filename for every new blob
var fileName = Guid.NewGuid().ToString();
CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(fileName);
headers.ContentLength = 0;
if (headers.ContentType != null)
{
// Set appropriate content type for your uploaded file
blob.Properties.ContentType = headers.ContentType.MediaType;
}
this.FileData.Add(new MultipartFileData(headers, blob.Name));
return blob.OpenWrite();
}
为了实现Oauth2.0身份验证,我刚刚在我的项目中添加了Startup .cs和Startup.Auth.cs(带有所需的NuGet包 Owin )
这是我的StackTrace
System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument参数,ExceptionResource资源),位于System.Collections.Generic.List
1.get_Item(Int32 index) at System.Net.Http.MultipartFormDataStreamProvider.<ExecutePostProcessingAsync>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpContentMultipartExtensions.<ReadAsMultipartAsync>d__0
1.MoveNext() ---在抛出异常的前一个位置的堆栈跟踪结束---在System.Runtime的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处。 CompilerServices.TaskAwaiter`1.GetResult() 在DemoAzureStorage.Controllers.UploadController.d__1.MoveNext()
答案 0 :(得分:0)
根据你的描述,我已经创建了一个测试演示(所有已安装的软件包都是最新的),它运行良好。
我使用了带谷歌登录的asp.net web api和AzureStorageMultipartFormDataStreamProvider作为你的节目。
我的上传文件控制器代码如下:
public class UploadController : ApiController
{
private const string Container = "mycontainer";
[HttpPost]
public async Task<IHttpActionResult> UploadFile()
{
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
var storageAccount = CloudStorageAccount.Parse("connection string");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer imagesContainer = blobClient.GetContainerReference(Container);
var provider = new AzureStorageMultipartFormDataStreamProvider(imagesContainer);
try
{
await Request.Content.ReadAsMultipartAsync(provider);
}
catch (Exception ex)
{
return BadRequest($"An error has occured. Details: {ex.Message}");
}
// Retrieve the filename of the file you have uploaded
var filename = provider.FileData.FirstOrDefault()?.LocalFileName;
if (string.IsNullOrEmpty(filename))
{
return BadRequest("An error has occured while uploading your file. Please try again.");
}
return Ok($"File: {filename} has successfully uploaded");
}
}
AzureStorageMultipartFormDataStreamProvider类:
public class AzureStorageMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
private readonly CloudBlobContainer _blobContainer;
private readonly string[] _supportedMimeTypes = { "image/png", "image/jpeg", "image/jpg" };
public AzureStorageMultipartFormDataStreamProvider(CloudBlobContainer blobContainer) : base("azure")
{
_blobContainer = blobContainer;
}
public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
if (parent == null) throw new ArgumentNullException(nameof(parent));
if (headers == null) throw new ArgumentNullException(nameof(headers));
if (!_supportedMimeTypes.Contains(headers.ContentType.ToString().ToLower()))
{
throw new NotSupportedException("Only jpeg and png are supported");
}
// Generate a new filename for every new blob
var fileName = Guid.NewGuid().ToString();
CloudBlockBlob blob = _blobContainer.GetBlockBlobReference(fileName);
if (headers.ContentType != null)
{
// Set appropriate content type for your uploaded file
blob.Properties.ContentType = headers.ContentType.MediaType;
}
this.FileData.Add(new MultipartFileData(headers, blob.Name));
return blob.OpenWrite();
}
结果如下图所示:
如果可能,您可以创建一个测试项目并将其推送到github或一个驱动器,以便我们重现您的问题。
Packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr" version="3.4.1.9004" targetFramework="net46" />
<package id="bootstrap" version="3.0.0" targetFramework="net46" />
<package id="EntityFramework" version="6.1.3" targetFramework="net46" />
<package id="jQuery" version="1.10.2" targetFramework="net46" />
<package id="Knockout.Validation" version="1.0.1" targetFramework="net46" />
<package id="knockoutjs" version="2.3.0" targetFramework="net46" />
<package id="Microsoft.ApplicationInsights" version="2.2.0" targetFramework="net46" />
<package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.0.6" targetFramework="net46" />
<package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.2.0" targetFramework="net46" />
<package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.2.0" targetFramework="net46" />
<package id="Microsoft.ApplicationInsights.Web" version="2.2.0" targetFramework="net46" />
<package id="Microsoft.ApplicationInsights.WindowsServer" version="2.2.0" targetFramework="net46" />
<package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.2.0" targetFramework="net46" />
<package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net46" />
<package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net46" />
<package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net46" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net46" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net46" />
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net46" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.5" targetFramework="net46" />
<package id="Microsoft.Data.Edm" version="5.8.2" targetFramework="net46" />
<package id="Microsoft.Data.OData" version="5.8.2" targetFramework="net46" />
<package id="Microsoft.Data.Services.Client" version="5.8.2" targetFramework="net46" />
<package id="Microsoft.Net.Compilers" version="2.1.0" targetFramework="net46" developmentDependency="true" />
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net46" />
<package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net46" />
<package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net46" />
<package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net46" />
<package id="Microsoft.Owin.Security.Facebook" version="3.0.1" targetFramework="net46" />
<package id="Microsoft.Owin.Security.Google" version="3.0.1" targetFramework="net46" />
<package id="Microsoft.Owin.Security.MicrosoftAccount" version="3.0.1" targetFramework="net46" />
<package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net46" />
<package id="Microsoft.Owin.Security.Twitter" version="3.0.1" targetFramework="net46" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net46" />
<package id="Modernizr" version="2.6.2" targetFramework="net46" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net46" />
<package id="Owin" version="1.0" targetFramework="net46" />
<package id="Respond" version="1.2.0" targetFramework="net46" />
<package id="Sammy.js" version="0.7.4" targetFramework="net46" />
<package id="System.ComponentModel.EventBasedAsync" version="4.0.11" targetFramework="net46" />
<package id="System.Dynamic.Runtime" version="4.0.0" targetFramework="net46" />
<package id="System.Linq.Queryable" version="4.0.0" targetFramework="net46" />
<package id="System.Net.Requests" version="4.0.11" targetFramework="net46" />
<package id="System.Spatial" version="5.8.2" targetFramework="net46" />
<package id="WebGrease" version="1.5.2" targetFramework="net46" />
<package id="WindowsAzure.Storage" version="8.5.0" targetFramework="net46" />
</packages>