1

我按照此tutorial从Azure Blob存储上传/下载Blob。下面“索引超出范围”等待时发生错误Request.Content.ReadAsMultipartAsync(provider)

代码工作完美,直到我实现了承载令牌认证(OAuth的)

我已经上载从postman.The任何文件得到了错误的错误描述。

{ “消息”:“发生了错误的细节:。索引超出范围必须大于 集合的大小非负少\ r \ n参数名称索引:”}

但是,这些文件已成功上传到我的Blob帐户中,但仍然出现错误。

我附上了调试时获得的错误详细信息的图像。

enter image description here

在那里我已经提示错误在我上传控制器

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身份验证我刚刚在我的项目中添加了启动.cs和Startup.Auth.cs(使用re quired的NuGet包Owin

这里是我的堆栈跟踪

在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.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)上System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在System.Runtime.CompilerServices .TaskAwaiter`1.GetResult() 在DemoAzureStorage.Controllers.UploadController.d__1.MoveNext()

+0

共享调用堆栈/堆栈跟踪。 – rene

+0

@rene更新了堆栈跟踪 – Jayendran

回答

0

根据你的描述,我创建了一个测试演示(所有已安装包是最新的一个)在我的身边,它工作得很好。

我用谷歌登录和AzureStorageMultipartFormDataStreamProvider作为您的节目使用asp.net web api。

我上传文件控制器代码如下:

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(); 
    } 

结果如下图所示:

enter image description here

如果可能的话,你可以创建一个测试项目并推送到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> 
+0

我已经在Github上发布了我的项目.https://github.com/jayendranarumugam/MyProject – Jayendran

+0

我已经使用了密码grant_type.So请使用以下access_token。{0} {0} {0} access_token“:” qOErEnLSbDjQW8XaUnSEImDNRTe8WRVJHbqXEUQxJw4EiqHoybw4Jc4gBYJkClORNCprnPbJxJ0WGumBcq-i4TMom-HxOBGgYgneYbTUKw5zyrWSwTEdtPsfjNzzYxCtcwSj4a9tEEj_j8czKcrauM9lhJMHcx4p9jS-nWkAAZepvJKJx8fqQUZgyo4tjysDSpNJBTAsTMgw2Z8xZ3QXGW06OzYW2LeOkNUmFp0PWY2sj0Suykgz0G2Lw7KXBar7okFHLV9KtZPArAWsOfFAvi-c84y6TjqzZkZnU5n5jmAy_Z3A3j1Ahww7Kz4p-p6lu7dYFw”, “token_type”: “承载” “expires_in”:60899, “refresh_token”: “a868cb78-4773-4fbf-888e-38a6b0a11700” } – Jayendran

+0

我不能在我身边运行你的应用程序。你能分享一个完整的应用程序吗?此外,我发现你的Owin版本是2.1,我建议你可以更新到3.1,然后再试一次。 –