Advertisement
Alisator

easyform_http

Apr 3rd, 2020
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. import logging
  2. import os
  3. import azure.functions as func
  4. from azure.storage.blob import BlobServiceClient, BlobClient
  5.  
  6.  
  7. def main(req: func.HttpRequest) -> func.HttpResponse:
  8.     logging.info("http")
  9.  
  10.     upload_file = req.params.get('upload')
  11.    
  12.     if not upload_file:
  13.         try:
  14.             req_body = req.get_json()
  15.         except ValueError:
  16.             pass
  17.         else:
  18.             upload_file = req_body.get('upload')
  19.  
  20.     if upload_file:
  21.         return func.HttpResponse(status_code=200,headers={'content-type':'text/html'},
  22.         body=
  23. """<!DOCTYPE html>
  24. <html>
  25.  <script type="text/javascript"
  26. src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.js">
  27. </script>
  28. <body>
  29.  
  30. <form enctype="multipart/form-data">
  31.    <input name="file" type="file" />
  32.    <input type="button" value="Upload" />
  33. </form>
  34. <progress></progress>
  35.  
  36. <script language="javascript" type="text/javascript">
  37. $(document).ready(function(){
  38.  
  39. $(':file').on('change', function () {
  40.  var file = this.files[0];
  41.  console.log(file)
  42.  
  43.  
  44. $(':button').on('click', function () {
  45.  var form = new FormData()
  46.  form.append('file',file)
  47.  
  48.  
  49.    $.ajax({
  50.    // Your server script to process the upload
  51.    url: 'https://functionappresttest.azurewebsites.net/api/returnNameTrigger?command=',
  52.    type: 'POST',
  53.    crossDomain: true,
  54.    enctype: 'multipart/form-data',
  55.    // Form data
  56.    data:form,
  57.  
  58.    // Tell jQuery not to process data or worry about content-type
  59.    // You *must* include these options!
  60.    cache: false,
  61.    contentType: false,
  62.    processData: false,
  63.  
  64.    success :  function(data){console.log(data);},
  65.  
  66.    // Custom XMLHttpRequest
  67.    xhr: function () {
  68.      var myXhr = $.ajaxSettings.xhr();
  69.      if (myXhr.upload) {
  70.        // For handling the progress of the upload
  71.  
  72.        myXhr.upload.addEventListener('progress', function (e) {
  73.          if (e.lengthComputable) {
  74.            $('progress').attr({
  75.              value: e.loaded,
  76.              max: e.total,
  77.            });
  78.          }
  79.        }, false);
  80.      }
  81.      return myXhr;
  82.    }
  83.  });
  84.  
  85. });
  86. """)
  87.  
  88.     else:
  89.         return func.HttpResponse(status_code=400)
  90.  
  91.     command = req.params.get('command')
  92.     logging.info('Python HTTP trigger function processed a request.')
  93.     try:
  94.         file=  req.files.get('file')
  95.         logging.info(file.filename)
  96.  
  97.         connect_str="your storage account connection string"
  98.         container="your container name"
  99.  
  100.         blob_service_client = BlobServiceClient.from_connection_string(connect_str)
  101.         blob_client =blob_service_client.get_blob_client(container=container,blob=file.filename)
  102.         blob_client.upload_blob(file)
  103.     except Exception as ex:
  104.         logging.info(ex.args)
  105.  
  106.     return func.HttpResponse(f"the file {file.filename} upload successfully")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement