python - 如何使用python在azure函数中使用http触发器上传html页面中的文件?

标签 python azure rest azure-functions

我想要一种方法,如何上传文件(可以是没有 php 的 html,或者一些交互式 azure 上传页面,等等),并通过我的 URL 参数发送参数,这将运行其余代码使用此上传的文件(ofc我至少需要将其保存到blob)。

我需要一个 REST API,所以我选择了 Azure Functions。

有什么办法可以在Python中做到这一点吗?我在 C# 中看到了很多示例,但 python 的文档很有限。

非常感谢!

最佳答案

针对该问题,您可以使用Html Form来实现。

例如

  1. HTML 页面
<!DOCTYPE html>
<html>
  <script type="text/javascript"
 src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.js">
</script>
<body>

 <form enctype="multipart/form-data">
    <input name="file" type="file" />
    <input type="button" value="Upload" />
</form>
<progress></progress>

<script language="javascript" type="text/javascript">
$(document).ready(function(){

$(':file').on('change', function () {
  var file = this.files[0];
  console.log(file)


$(':button').on('click', function () {
  var form = new FormData()
  form.append('file',file)


    $.ajax({
    // Your server script to process the upload
    url: '<your azure function app url>',
    type: 'POST',
    crossDomain: true,
    enctype: 'multipart/form-data',
    // Form data
    data:form,

    // Tell jQuery not to process data or worry about content-type
    // You *must* include these options!
    cache: false,
    contentType: false,
    processData: false,

    success :  function(data){console.log(data);},

    // Custom XMLHttpRequest
    xhr: function () {
      var myXhr = $.ajaxSettings.xhr();
      if (myXhr.upload) {
        // For handling the progress of the upload

        myXhr.upload.addEventListener('progress', function (e) {
          if (e.lengthComputable) {
            $('progress').attr({
              value: e.loaded,
              max: e.total,
            });
          }
        }, false);
      }
      return myXhr;
    }
  });

});





});



});

</script>

</body>
</html>
  • 函数代码(将文件上传到 Azure blob)
  • import logging
    import os
    import azure.functions as func
    from azure.storage.blob import BlobServiceClient, BlobClient
    def main(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
        try:
            file=  req.files.get('file')
            logging.info(file.filename)
    
            connect_str="your storage account connection string"
            container="your container name"
    
            blob_service_client = BlobServiceClient.from_connection_string(connect_str)
            blob_client =blob_service_client.get_blob_client(container=container,blob=file.filename)
            blob_client.upload_blob(file)
        except Exception as ex:
            logging.info(ex.args)
    
        return func.HttpResponse(f"the file {file.filename} upload successfully")
    
  • 为您的功能配置 CORS enter image description here

  • 测试 enter image description here

  • 关于python - 如何使用python在azure函数中使用http触发器上传html页面中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60889319/

    相关文章:

    python - 如何高效统计人名中两个单词组合的数量?

    c# - 如何使用安全声明代表 AAD 用户调用 Web API?

    c# - Azure设置内部url的可用性测试

    javascript - 如何将 WebSocket 连接与 Socket.io 事件和 Koa Rest api 结合使用?

    python - 使用 Cython 构建未知长度的一维数组/列表/vector 的最有效方法?或者永远不应该这样做?

    python - 2 个列表中字典组合的所有可能排列

    ios - 如何生成 Google Place API session token ?

    python - 使用数据库后端测试 REST API

    python - 在 R 中使用 system() 调用 python 以运行模拟 python 控制台的 python 脚本

    .net - ASP.NET MVC 5 Web 应用程序,命名空间不存在?