"Index was out of range" 等待时出错 Request.Content.ReadAsMultipartAsync(提供商)
"Index was out of range" Error at await Request.Content.ReadAsMultipartAsync(provider)
我从 Azure Blob 存储中关注 tutorial 到 upload/download 个 blob。
在我实现 Bearer 令牌身份验证 (OAuth) 之前,代码可以完美运行
我在从 postman.The 上传任何文件时遇到错误,下面是错误描述。
{
"Message": "An error has occurred. Details: Index was out of range. Must be non-negative and less than the size of the
collection.\r\nParameter name: index" }
然而,文件已成功上传到我的 Blob Account.But,但我仍然不断出现错误。
我已附上调试时获得的错误详细信息的图像。
我的上传控制器提示错误的地方
try
{
await Request.Content.ReadAsMultipartAsync(provider);
}
catch (Exception ex)
{
return BadRequest($"An error has occured. Details: {ex.Message}");
}
我的 AzureStorageMultipartFormDataStreamProvider Class 继承自 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
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) at System.Collections.Generic.List1.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()
--- 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.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at DemoAzureStorage.Controllers.UploadController.d__1.MoveNext()
根据你的描述,我自己制作了一个测试demo(所有安装的包都是最新的),效果很好。
我使用 asp.net web api google 登录和 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 Class:
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>
我从 Azure Blob 存储中关注 tutorial 到 upload/download 个 blob。
在我实现 Bearer 令牌身份验证 (OAuth) 之前,代码可以完美运行
我在从 postman.The 上传任何文件时遇到错误,下面是错误描述。
{ "Message": "An error has occurred. Details: Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index" }
然而,文件已成功上传到我的 Blob Account.But,但我仍然不断出现错误。
我已附上调试时获得的错误详细信息的图像。
我的上传控制器提示错误的地方
try
{
await Request.Content.ReadAsMultipartAsync(provider);
}
catch (Exception ex)
{
return BadRequest($"An error has occured. Details: {ex.Message}");
}
我的 AzureStorageMultipartFormDataStreamProvider Class 继承自 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
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) at 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() --- 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.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at DemoAzureStorage.Controllers.UploadController.d__1.MoveNext()
根据你的描述,我自己制作了一个测试demo(所有安装的包都是最新的),效果很好。
我使用 asp.net web api google 登录和 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 Class:
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>