如何使用 javascript 从浏览器将文本字符串直接上传到 Windows Azure Blob [英] How to upload a text string directly to Windows Azure Blob from the browser using javascript

查看:27
本文介绍了如何使用 javascript 从浏览器将文本字符串直接上传到 Windows Azure Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用浏览器端 javascript 从 Web 浏览器将文本/html 字符串作为 Azure blob 直接上传到 Azure 存储(无需先通过 Azure VM/Web 服务器).根据我的研究,我了解到可以使用 Azure REST API + 共享访问签名从 Silverlight 客户端或通过 MVC 3 视图实现这一点.(这里是使用 SilverLight/MVC3 的控件的链接:http:///code.msdn.microsoft.com/silverlight/Silverlight-Azure-Blob-3b773e26#content).我还找到了一个使用服务器端 javascript 和 Node.JS 上传的库 https://github.com/jpgarcia/waz-storage-js .

I would like to upload from a web browser a text/html string as an Azure blob directly to Azure storage (without first going thru the Azure VM/webserver) using browser side javascript. From my research, I understand that one can use Azure REST API + Shared Access Signatures to achieve this from a Silverlight client or thru an MVC 3 view. ( here is the link for controls using SilverLight/MVC3 : http://code.msdn.microsoft.com/silverlight/Silverlight-Azure-Blob-3b773e26#content). I also found a library for uploading using server side javascript with Node.JS https://github.com/jpgarcia/waz-storage-js .

我的问题是,如何仅使用浏览器端 javascript 和 html 和 SAS(不使用 Silverlight 或 MVC)将文本字符串上传到 Azure blob,而无需通过 Web 服务器直接上传到 Azure?如何将字符串值传递给 Azure rest api 调用(它会接受 JSON 字符串吗?)?我查看了 MSDN REST api 示例,但我不清楚您将如何传递要上传的文本字符串.有人能帮忙吗?提前致谢.

My question is how would one upload a text string to an Azure blob using only browser side javascript and html and SAS (without using Silverlight or MVC) and do so directly to Azure without going thru the web server? How would one pass the string value to the Azure rest api call ( would it accept a JSON string?) ? I looked at the MSDN REST api examples but it was not clear to me how you would pass a text string to be uploaded. Can anyone help with this? Thanks in advance.

推荐答案

我不认为这是可以做到的.除非网页是从同一个 blob 存储域托管的,否则这将是一个跨域调用,这是不允许的(除非设置了 CORS 标头,它们不适用于 blob 存储).

I don't think this can be done. Unless the web page is being hosted from the same blob storage domain, this would be a cross-domain call, which isn't allowed (unless CORS headers are set, which they aren't for blob storage).

另外,上传到 blob 需要一个 HTTP PUT 请求,我相信大多数浏览器只允许 POST 和 GET.

In addition, uploading to a blob requires an HTTP PUT request, and I believe most browsers only allow POST and GET.

更新

请参阅下面的评论,但看起来某些浏览器确实允许 PUT 请求.如前所述,可以通过从同一 Blob 存储帐户提供原始页面来破坏同源策略.在这种情况下,代码看起来像这样(使用 jQuery):

See comments below, but it looks like some browsers do allow PUT requests. As noted, the same-origin policy can be defeated by serving the original page from the same blob storage account. In that case, the code would look something like this (using jQuery):

$.ajax({
    url: url,
    type: 'PUT',
    data: 'Hello, World!',
    headers: {'Content-Type': 'text/html'}
}).success(function () {
    console.log('Saved successfully.');
});

这篇关于如何使用 javascript 从浏览器将文本字符串直接上传到 Windows Azure Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆