javascript - 如何将文本文件从 Web 服务器写入 Azure 文件共享?

标签 javascript php azure cors

我们目前有一个网页,在提交表单时使用 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 规则。 .

enter image description here enter image description here

<小时/>

更新:

感谢 Gaurav 在评论中的提醒。当您使用 Javascript 时,最好使用 SAS(共享访问签名)来创建文件。 SAS 在前端使用起来比 SharedKey 更容易、更安全。您可以按照以下步骤来执行此操作。

  1. 从 Azure 门户生成 SAS 并复制 SAS token :

enter image description here

  • 调用 https://sofyvhdfiles.file.core.windows.net/sofyvhd/test.txt + SAS token使用 HTTPS PUT 和 x-ms-typex-ms-content-length :
  • enter image description here

    关于javascript - 如何将文本文件从 Web 服务器写入 Azure 文件共享?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45495249/

    相关文章:

    syntax - PHP类方法中使用的 undefined variable 没有错误

    azure - 在Azure表存储中,CreateIfNotExists算作事务吗?

    azure - 使用 AzCopy 将表从 azure 复制到本地存储

    javascript - 使用 Jest 和 Sinon 的 React 测试函数调用不起作用

    php - 无法在服务器上运行 Zend Framework 2 - 找不到类 'Zend\Mvc\Application'

    javascript - 日期 fns 的 isSameOrBefore 实现

    php - 向数据库插入数据时出错

    asp.net - web.config 重写规则在 Blob 存储中的 Azure 静态网站中不起作用

    javascript - 加载和显示 svg

    javascript - Angularjs 获取选中的复选框元素