Sending files from web app to device

Alex Fang 230 Reputation points
2023-08-14T17:31:34.4166667+00:00

I know that it is possible to upload files from the device to the IoT Hub as shown here.

Is it possible to upload/send a file from a backend web application to the device? For example, if I want to send a configuration file to the device to use accordingly. As of right now, I am just sending file contents as a C2D message, but is it possible to send an actual file?

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,258 questions
{count} votes

Accepted answer
  1. Sander van de Velde | MVP 36,131 Reputation points MVP
    2023-08-14T18:23:31.0166667+00:00

    Hello @Alex Fang,

    you are currently using a C2D message which is part of the IoT Hub cloud-to-device communication strategies:

    • DeviceTwin; send desired properties and read reported properties. The device does not have to be connected at that moment. It can read and write properties when it connects to the cloud at a later moment
    • Direct method; Send a message body and receive a response body once the method is executed on the device. The device must be connected to the cloud while the method call is made. A method can time-out.
    • Cloud messages; queue one or more cloud messages which can be picked up by a device once it connects to the cloud. Each message must be accepted to be taken from the queue. Cloud messages do not return a response (eg. use reported properties on the device twin)

    As you can see, sending a file could be hard using one of these solutions due to message size restrictions.

    Still, your question is a common one and has there is a common pattern to solve this.

    Put the files you want to send to a device in a blob storage and send the device a desired property with the SAS token so the device can download it over HTTPS:

    User's image

    SAS tokens can expire so you could set the expiration date far into the future.

    A more secure solution is when a device has no access to the file anymore (SAS is expired), It sends a custom message to the cloud requesting a new SAS. Then, some cloud logic (like an Azure Function) creates a new SAS token and updates the desired property.

    This is the most flexible way to download small or large messages in a secure way.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.