0

I have a url with a SAS token to upload a file. I have a base64 string of a previously defined content type that I need to upload.

import { FileUploadStreamOptions, ShareFileClient } from "@azure/storage-file-share";
import { Readable } from "readable-stream";

async submitData(context: ComponentFramework.Context<IInputs>, fileName: string, ext: string, base64: string, contentType: string, classification: string, upload: any) : Promise<boolean> {
   const buffer = Buffer.from(base64, 'base64');

   let client = new ShareFileClient(upload.url);
   let readable = Readable.from(buffer);

   const options = {
       fileHttpHeaders: { 
          fileContentType: contentType,
       },
   };

   await client.uploadStream(readable, buffer.length, 4 * 1024 * 1024, 5, options);
   console.log("Upload successful");
   
   // more logic
   return true;
}

I'm having two problems with this code:

  1. The await client.uploadStream(readable, buffer.length, 4 * 1024 * 1024, 5, options); is not being awaited and the method is escaped.

  2. The uploaded file is corrupted. When downloaded, the content comes back as:

{"$content-type":"application/pdf","$content":"AAAAAAAAAAAAAA...AAAAA"}
  1. I have tried not awaiting the call and that seems to work for the most part although it really should be awaited.

  2. I tried adding encoding to the stream but that didn't seem to work either.

2
  • You need to upload larger files to azure file share using react js is it right? Commented Oct 22, 2024 at 3:32
  • Check the below answer. Commented Oct 25, 2024 at 5:39

1 Answer 1

0

I have a url with a SAS token to upload a file. I have a base64 string of a previously defined content type that I need to upload.

You can use the below code to upload the pdf file in the Azure file share.

Code:

// upload.ts
import { ShareFileClient, FileUploadStreamOptions } from "@azure/storage-file-share";
import { Readable } from "stream"; 

async function submitData(fileName: string, base64: string, contentType: string, uploadUrl: string): Promise<boolean> {
    try {
        const buffer = Buffer.from(base64, 'base64');
        const client = new ShareFileClient(uploadUrl);
        const readable = Readable.from(buffer);
        
        const options: FileUploadStreamOptions = {
            fileHttpHeaders: { 
                fileContentType: contentType,
            },
        };
        await client.uploadStream(readable, buffer.length, 4 * 1024 * 1024, 5, options);
        console.log("Upload successful");
        
        return true;
    } catch (error) {
        console.error("Upload failed:", error);
        return false;
    }
}

const main = async () => {
    const fileName = "example.pdf"; // Set the file name
    const contentType = "application/pdf"; // Set the content type

    const uploadUrl = "https://<storage account name>.file.core.windows.net/<share name>/<filename>?sv=2022-11-02&ss=bfqt&srt=sco&sp=rwdlacupiytfx&se=2024-10-22T16:45:20Z&st=2024-10-22T08:45:20Z&spr=https&sig=redacted"; 

    const base64 = "6xxxxx"
    const result = await submitData(fileName, base64, contentType, uploadUrl);
    console.log(`Upload result: ${result}`);
};

main().catch(console.error);

The above explain with submitData function converts the base64 string to a buffer, creates a ShareFileClient, and uploads the file using the uploadStream method. It returns true if the upload is successful, otherwise it returns false.

Output:

Upload successful
Upload result: true

The above code successfully uploaded the file, and when I clicked Download from the portal after uploading, it opened without any problems.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.