【发布时间】:2017-10-30 05:37:46
【问题描述】:
我正在使用 React 版本的 FineUploader 将文件上传到 Azure blob 存储,并且上传器给了我以下错误:
请求中没有“Access-Control-Allow-Origin”标头 资源。
我使用以下代码设置后端来设置 CORS 设置。此代码是来自 FineUploader 的代码示例的略微修改版本: https://github.com/FineUploader/server-examples/blob/master/C%23/azure/FineUploaderAzureServer.cs
public async Task ConfigureCors()
{
var ALLOWED_CORS_ORIGINS = new List<String> { "http://myapp.com", "http://localhost:3333"};
var ALLOWED_CORS_HEADERS = new List<String> { "x-ms-meta-qqfilename", "Content-Type", "x-ms-blob-type", "x-ms-blob-content-type" };
const CorsHttpMethods ALLOWED_CORS_METHODS = CorsHttpMethods.Delete | CorsHttpMethods.Put | CorsHttpMethods.Options;
const int ALLOWED_CORS_AGE_DAYS = 5;
var properties = await _client.GetServicePropertiesAsync();
properties.DefaultServiceVersion = "2013-08-15";
await _client.SetServicePropertiesAsync(properties);
var addRule = true;
if (addRule)
{
var ruleWideOpenWriter = new CorsRule()
{
AllowedHeaders = ALLOWED_CORS_HEADERS,
AllowedOrigins = ALLOWED_CORS_ORIGINS,
AllowedMethods = ALLOWED_CORS_METHODS,
MaxAgeInSeconds = (int)TimeSpan.FromDays(ALLOWED_CORS_AGE_DAYS).TotalSeconds
};
properties.Cors.CorsRules.Clear();
properties.Cors.CorsRules.Add(ruleWideOpenWriter);
await _client.SetServicePropertiesAsync(properties);
}
}
上面的代码工作正常。当我登录到我的 Azure 门户时,我会在我的存储帐户的 CORS 选项卡下看到这些设置。
在我研究的过程中,我注意到有人提出问题可能是由于来自 localhost 的请求。我将应用程序移至 Azure 网站进行测试,但仍然遇到同样的错误。我确实在我的 CORS 设置中包含了这个测试站点的新 URL。
这是我很好的上传器包装器初始化:
const uploader = new FineUploaderAzure({
options: {
cors: {
expected: true,
sendCredentials: true
},
signature: {
endpoint: 'http://localhost:49065/getsas'
},
request: {
endpoint: 'https://myaccount.blob.core.windows.net/test-container'
},
uploadSuccess: {
endpoint: 'http://localhost:49065/success'
}
}
})
知道是什么导致了这个问题吗?
【问题讨论】:
-
请单独显示确切的请求 url 和标头,并准确显示每个 azure 的 cors 规则
-
在原始帖子中添加了屏幕截图。
-
我还需要根据浏览器开发工具的请求信息、标头等
-
您可以转到ingridfileuploader.azurewebsites.net,这是我在localhost:3333 运行的本地反应应用程序的精确副本。 localhost:49065 是我在本地运行的 API 应用程序的精确副本。
-
刚刚添加了来自 Chrome 的请求标头的屏幕截图。此外,如果我没有提供您需要的所有内容,您可以更轻松地前往ingridfileuploader.azurewebsites.net 并尝试上传文件。
标签: cors fine-uploader