Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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("http")
- upload_file = req.params.get('upload')
- if not upload_file:
- try:
- req_body = req.get_json()
- except ValueError:
- pass
- else:
- upload_file = req_body.get('upload')
- if upload_file:
- return func.HttpResponse(status_code=200,headers={'content-type':'text/html'},
- body=
- """<!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: 'https://functionappresttest.azurewebsites.net/api/returnNameTrigger?command=',
- 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;
- }
- });
- });
- """)
- else:
- return func.HttpResponse(status_code=400)
- command = req.params.get('command')
- 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")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement