我们目前有一个网页,在提交表单时使用 PHP 并在网络服务器上创建一个文本文件。我们希望将此文本文件写入 Azure 文件共享(而不是 blob),以便我们可以从 VM 访问它。我们如何着手做到这一点,我们尝试过使用 HTTPS POST 请求,但在 CORS 方面遇到了困难,现在我们迷失了方向。
function sendFile(){
var xhttp = new XMLHttpRequest();
xhttp.open("PUT","https://sofyvhdfiles.file.core.windows.net/sofyvhd/test.txt",false);
xhttp.setRequestHeader("Authentication", "SharedKey myaccount:[Key Here]");
xhttp.setRequestHeader("x-ms-version","2017-04-17");
xhttp.setRequestHeader("x-ms-content-length",102400);
xhttp.setRequestHeader("x-ms-type","text/plain; charset=UTF-8");
xhttp.send();
var response = JSON.parse(xhttp.responseText);
console.log(response);
}
sendFile();
最佳答案
您可以使用 Azure portal 设置文件存储服务的 CORS 规则。 .
<小时/>更新:
感谢 Gaurav 在评论中的提醒。当您使用 Javascript 时,最好使用 SAS(共享访问签名)来创建文件。 SAS 在前端使用起来比 SharedKey 更容易、更安全。您可以按照以下步骤来执行此操作。
- 从 Azure 门户生成 SAS 并复制
SAS token
:
- 调用
https://sofyvhdfiles.file.core.windows.net/sofyvhd/test.txt
+SAS token
使用 HTTPS PUT 和x-ms-type
和x-ms-content-length
:
关于javascript - 如何将文本文件从 Web 服务器写入 Azure 文件共享?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45495249/