将xls文件上传到Azure Data Lake Gen1时不支持获取文件格式

时间:2019-03-20 13:26:07

标签: angular azure-data-lake

我正在开发一个应用程序,它将使用angular7.net core接收用户的文件输入。我正在使用网络套接字将有效负载传递到后端。我可以成功上传到azure datalake,但是在预览或下载文件时,文件已损坏,文件大小也大于原始文件大小。

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 ..........";
    }
  }

之后,我将有效负载传递给websocket,如下所示:

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

我的控制器:

 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)
            {



            }
        }

1 个答案:

答案 0 :(得分:0)

是的,我尝试重现相同的场景并遇到相同的错误。

似乎,从门户网站下载时出现了此问题,但是如果遇到问题,可以使用Azure存储资源管理器或AZ cli下载文件。

Azure Storage Explorer:

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

Powershell示例:

https://docs.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

希望有帮助。