cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Frequent Visitor

How to upload Excel files to SharePoint via HTTP

Hello, I am trying to upload files to SharePoint using HTTP. I have succeeded to upload text files to a SharePoint folder without issues, but when I upload Excel files, at opening the Excel files, I get an error saying that the excel file is corrupt. I think it is a problem with how to convert the binary content in a way that is understandable by Excel.

 

FLOW COMPONENTS

1. HTTP POST will give you the URL that you should call to send the file content.

Copy the URL that you will get from this component, as soon as you save your flow. This is the URL that you will have to call to send the file to.

2. Parse JSON with a schema that reads the site_address, application_name, and target_path and file content (see it below)

{
    "type": "object",
    "properties": {
        "site_address": {
            "type": "string"
        },
        "target_path": {
            "type": "string"
        },
        "file_name": {
            "type": "string"
        },
        "file_content": {
            "type": "string"
        }
    }
}

3. SharePoint Create File

The Json variables read in the step below to feed site_address (the URL of the target SharePoint site), folder_path (The path to the library/folder in SharePoint where you will save your file), file_name (the name of the file you will upload) and file_content (the content of your file).

 

upload_files_to_sharepoint_via_http.JPG

 

The Python code to upload the file to SharePoint is shown below:

import requests
import json

def upload_text_file_to_powerautomate(site_address, file_location, file_name, target_path):
    """
        Description: uploads a text file to a SharePoint folder, whose path is specified using a PowerAutomate flow.
            Make sure the flow is turned on
        :site_address: SharePoint site address
        :target_path: Path to the folder in the SharePoint site where the file will be uploaded to
        :file_location: Local folder in the sending computer that is sending the file
        :file_name: Name of the file that will be uploaded
    """
    power_automate_url = "<the_url_from_your_power_automate_https_component"    # copy / paste this URL from PowerAutomate HTTPS component
    full_file_location = os.path.join(file_location, file_name) # puts together the file location and file name
    with open(full_file_location, 'rb') as f:
        file_content = f.read()             # read the content of the file
        data = {
            'site_address': site_address,
            'target_path': target_path,
            'file_name': file_name,
            'file_content': file_content
        }
        data_json = json.dumps(data)        # converts the dictionary to a json format
        r = requests.post(power_automate_url, json= data_json)  # uploads the json data to the PowerAutomate HTTP web service

# calling the function to upload the file to SharePoint
upload_text_file_to_powerautomate(
    site_address= "https://<your_target_sharepoit_site>"
    file_location= "<where_you_have_the_file_to_upload>, e.g.c:\my_folder_location"
    target_path= "your_target_path, e.g. /Shared Documents/test"
    file_name= "my_text_file.txt"
)

 the code above uploads text files and also can upload Excel file, but the problem I get is when I upload an excel file to SharePoint and I try to open the Excel file, I get an error saying that the file is corrupt 

 

 I am pretty sure is because somehow I have to decode the binary content in a way that Excel is able to understand it. If someone knows how to convert this content, we have the full solution to upload Excel files to SharePoint using HTTP. Any help is greatly appreciated.

 

Best regards,

- Noah

1 ACCEPTED SOLUTION

Accepted Solutions

Thank you for your input. I did several tests, did changes in the Python script, and also in the Power Automate flow. In the end, only two components are needed in the Power Automate flow: an HTTP post and a SharePoint Create File. The HTTP module should be a POST and it is not necessary to indicate a schema. The schema can be left blank, as shown below. The URL that Python should send the request to comes from the HTTP indicated below, just copy/paste it to the Python script. About the SharePoint Create File module, the file content value comes from a custom expression. You were correct, I had to use base64ToBinary. The complete expression is below:

 

base64ToBinary(triggerBody()?['$content'])

 

create_excel_files_via_http_post.jpg

About the Python code, I found the answer in this StackOverflow post.

 

the code I use is:

 

import requests

def send_excel_to_powerautomate():
    file = r"test.xlsx"         # excel file that you want to upload to Power Automate
    binary_upload_url = "https://prod-20..." # url that you copy/paste form the HTTP module in PowerAutomate
    data = open(file, 'rb').read()
    res = requests.post(binary_upload_url,
                    data=data,
                    headers={'Content-Type': 'application/octet-stream'})
    print(res.status_code)
    print(res.text)

send_excel_to_powerautomate()

 

It worked like a charm!!! With this method, Excel files can be uploaded to SharePoint using an HTTP. Probably any kind of binary files can be uploaded, but I have only tested it yet with Excel files. Thank you for your help.

 

- Noah

View solution in original post

2 REPLIES 2
Microsoft
Microsoft

@N0AH 

 

Could you please try using the function base64tobinary() to convert the content of the file to binary format.

Also if the file content is coming in URI format then you can also try using Datauritobinary().

 

Please try using the above functions to convert the file content into binary format and pass the converted data as parameter to sharepoint action.

 

https://docs.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#c...

 

Please check the above link for more information related to conversion functions in power automate.

 

If this information helps you with resolution, please consider giving a thumbs up and mark the solution as resolved.

Thank you for your input. I did several tests, did changes in the Python script, and also in the Power Automate flow. In the end, only two components are needed in the Power Automate flow: an HTTP post and a SharePoint Create File. The HTTP module should be a POST and it is not necessary to indicate a schema. The schema can be left blank, as shown below. The URL that Python should send the request to comes from the HTTP indicated below, just copy/paste it to the Python script. About the SharePoint Create File module, the file content value comes from a custom expression. You were correct, I had to use base64ToBinary. The complete expression is below:

 

base64ToBinary(triggerBody()?['$content'])

 

create_excel_files_via_http_post.jpg

About the Python code, I found the answer in this StackOverflow post.

 

the code I use is:

 

import requests

def send_excel_to_powerautomate():
    file = r"test.xlsx"         # excel file that you want to upload to Power Automate
    binary_upload_url = "https://prod-20..." # url that you copy/paste form the HTTP module in PowerAutomate
    data = open(file, 'rb').read()
    res = requests.post(binary_upload_url,
                    data=data,
                    headers={'Content-Type': 'application/octet-stream'})
    print(res.status_code)
    print(res.text)

send_excel_to_powerautomate()

 

It worked like a charm!!! With this method, Excel files can be uploaded to SharePoint using an HTTP. Probably any kind of binary files can be uploaded, but I have only tested it yet with Excel files. Thank you for your help.

 

- Noah

View solution in original post

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

MPA Community Blog

Power Automate Community Blog

Check out the community blog page where you can find valuable learning material from community and product team members!

Users online (13,071)