Vikas Singh
Vikas Singh

Reputation: 181

Getting File Format not supported while uploading xls file to azure data lake gen1

I am developing an application which will take file input from the user using angular7 and .net core. I am passing payload to my backend using a websocket. I am able to upload to azure datalake successfully, but when I am previewing or downloading the file it is currupted and the file size is also higher than the orignal file size.

upload.html

<input id="file-input" accept=".xlsx" type="file" style="display: none"  (change)="addExternalFile($event)" />

upload.ts

  addExternalFile(event) {
    try {

      this.myForm1 = false;
      this.addFlag = false;
      this.FileName = "";
      this.first_sheet_name = "";
      this.SheetNames = "";
      this.file = event.target.files[0];
      console.log(event.target.value);
      let fileReader = new FileReader();
      fileReader.onload = (e) => {
        this.arrayBuffer = fileReader.result;

         this.Filedata = new Uint8Array(this.arrayBuffer);
        var arr = new Array();
        for (var i = 0; i != this.Filedata.length; ++i) arr[i] = String.fromCharCode(this.Filedata[i]);
        var bstr = arr.join("");
        this.workbook = XLSX.read(bstr, { type: "binary" });
        this.first_sheet_name = this.workbook.SheetNames[0];
        this.SheetNames = this.first_sheet_name;
        this.WorksheetNamelist = this.workbook.SheetNames;
        if (this.Filedata.length > 0) {
          this.FileName = this.file.name;
          this.FileType = this.file.type;
          this.addFlag = true;
          this.myForm1 = true;
          this.UploadStatus = "Upload Successfully";
          this.Upload(event);
        }
        else {
          this.UploadStatus = "Upload Failed , Please Try Again ..........";
        }
      }
      fileReader.readAsArrayBuffer(this.file);


    }
    catch (ex) {
      this.UploadStatus = "Upload Failed , Please Try Again ..........";
    }
  }

After that I am passing the payload to the websocket like this:

var data = { "fileName": this.FileName, "Path": "", "sheetName": this.Sheetlist, "filetype": this.FileType, "Filedata": this.Filedata };
      var payload = JSON.stringify(data);
       this.commonService.sendMessageToServer(payload);

My controller:

 public async Task<string> CreateDataSetFromFile(MessageWrapper wrapper)
        {
            var tenantname = wrapper.TenantName;
            var tenantID = wrapper.TenantID;
            AdlCred adlcred = new AdlCred();
            adlcred._adlsAccountName = "";     
            string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            string fileName = jobject["fileName"].ToString();


            string pathname = "/upload/" + fileName;

            //JObject tempArray = JObject.Parse( jobject["Filedata"].ToString());
            string stringData = jobject["Filedata"].ToString();



            //byte[] Filedata = Encoding.ASCII.GetBytes(jobject["Filedata"].ToString());
            byte[] Filedata =  Encoding.ASCII.GetBytes(jobject["Filedata"].ToString());

           // string[] arrayString = new string[]; //Your Array.

            byte[] arrayByte = new byte[Filedata.Length];

            for (int i = 0; i < arrayByte.Length; i++)
            {
                arrayByte[i] = Convert.ToByte(arrayByte[i]);
            }
            Stream stream = new MemoryStream(arrayByte);

         //   MemoryStream stream = new MemoryStream(Filedata);
            string sname = "Sheet1";
            bool extHeader = true;
            bool hasMerCells = false;


            await Create(stream, pathname, true, adlcred);
}

 public async Task Create(Stream stream, string destFilePath, bool overWridden, AdlCred adlcred)
        {
            try
            {




                //  var creds = ApplicationTokenProvider.LoginSilentAsync(adlcred._domain, adlcred._applicationId, adlcred._secretKey).Result;

                var creds = ApplicationTokenProvider.LoginSilentAsync("", "", "").Result;

                DataLakeStoreFileSystemManagementClient _adlsFileSystemClient = new DataLakeStoreFileSystemManagementClient(creds);



                await _adlsFileSystemClient.FileSystem.CreateAsync("althing", destFilePath, stream,true);
                // _adlsFileSystemClient.FileSystem.Append(adlcred._adlsAccountName, destFilePath, stream);
                //  await _adlsFileSystemClient.FileSystem.CreateAsync(adlcred._adlsAccountName, destFilePath, stream, overWridden);
                Console.WriteLine("File Uploaded Successfully...");
            }
            catch (Exception ex)
            {



            }
        }

Upvotes: 0

Views: 251

Answers (1)

Mohit Verma
Mohit Verma

Reputation: 5294

Yes , i tried to reproduce the same scenario and got the same error.

Seems ,downloading from portal is giving this issue, though if your stuck you can use Azure storage explorer or AZ cli to download the file.

Azure Storage Explorer:

https://azure.microsoft.com/en-us/features/storage-explorer/

Powershell examples:

https://learn.microsoft.com/en-us/azure/data-lake-store/data-lake-store-get-started-powershell#rename-download-and-delete-data-from-your-data-lake-storage-gen1-account

Hope it helps.

Upvotes: 1

Related Questions