从cordova应用程序将图像上载到Azure Blob时出错

时间:2016-01-19 15:33:53

标签: c# cordova azure

我正在尝试从Cordova应用程序上传一个blob,而我正在获得404.但是,SAS URL是有效的,并且可以正常使用C#应用程序。 请找到以下代码:

var uriWithAccess = URL;
var xhr = new XMLHttpRequest();
xhr.onerror = fail;
xhr.onloadend = uploadCompleted;
xhr.open("POST", uriWithAccess);
xhr.setRequestHeader('x-ms-blob-type', 'BlockBlob');
xhr.setRequestHeader('x-ms-blob-content-type','image/jpeg');
xhr.send(requestData);

任何帮助将不胜感激。我也试过$ .ajax,但它也给出了404错误。

PS:代码工作得非常好但是从最近几天开始导致问题。

谢谢,

Mohit Chhabra

1 个答案:

答案 0 :(得分:0)

您是否配置了CORS?也许js请求失败了,因为在azure存储中不允许执行域。

<Cors>    
      <CorsRule>
            <AllowedOrigins>http://www.contoso.com, http://www.fabrikam.com</AllowedOrigins>
            <AllowedMethods>PUT,GET</AllowedMethods>
            <AllowedHeaders>x-ms-meta-data*,x-ms-meta-target*,x-ms-meta-abc</AllowedHeaders>
            <ExposedHeaders>x-ms-meta-*</ExposedHeaders>
            <MaxAgeInSeconds>200</MaxAgeInSeconds>
    </CorsRule>
<Cors>

要设置该配置,您可以使用azure Storage REST API,或者更轻松地运行这样的简短C#程序:

private static void InitializeCors()
{
     // CORS should be enabled once at service startup
     // Given a BlobClient, download the current Service Properties 
     ServiceProperties blobServiceProperties = BlobClient.GetServiceProperties();
     ServiceProperties tableServiceProperties = TableClient.GetServiceProperties();

     // Enable and Configure CORS
     ConfigureCors(blobServiceProperties);
     ConfigureCors(tableServiceProperties);

     // Commit the CORS changes into the Service Properties
     BlobClient.SetServiceProperties(blobServiceProperties);
     TableClient.SetServiceProperties(tableServiceProperties);
}

private static void ConfigureCors(ServiceProperties serviceProperties)
{
    serviceProperties.Cors = new CorsProperties();
    serviceProperties.Cors.CorsRules.Add(new CorsRule()
    {
        AllowedHeaders = new List<string>() { "*" },
        AllowedMethods = CorsHttpMethods.Put | CorsHttpMethods.Get | CorsHttpMethods.Head | CorsHttpMethods.Post,
        AllowedOrigins = new List<string>() { "*" },
        ExposedHeaders = new List<string>() { "*" },
        MaxAgeInSeconds = 1800 // 30 minutes
     });
}

我不确定您应该使用什么主机来启用对移动应用程序的访问权限,但首先您应该尝试使用所有主机。

Access-Control-Allow-Origin: *

AllowedOrigins = new List<string>() { "*" },

您可以在此处按照详细指南进行操作:

Windows Azure Storage: Introducing CORS