How to get excel file from disc in angular2 via rest - excel

I am using an angular 2 with Java background and communication between them is through REST. What I have to do is to create some excel file on button click and then to return that file in the user API.
REST looks like this:
#RequestMapping(value = "/some_path/{someId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public FileSystemResource exportSomeData(#PathVariable long someId, HttpServletResponse response) {
// ... create excel file data...
File file = new File(fileName);
response.addHeader("FILE_NAME", fileName);
FileNameResource fsr = new FileNameResource(file);
return fsr;
}
In angular (return of the REST, call works ok):
getFile(path:String) {
this.autthHttp.get(`some_path')
.map((response) => {
let blob = (response)['body'];
return {
data: new Blob([blob], {type: 'application/octet-stream'}),
filename: response.headers.get('FILE_NAME')
}
})
.subscribe(res => saveAs(res.data, res.filename))
}
The problem is that I got the file, it contains data, but it lost it's metadata (show some question mark characters instead of format excel well in cells). Does somebody knows what can be the problem?

Try to set the responseType to Blob and use RC5 blob response type:
getFile(path:String) {
this.autthHttp.get(`some_path', {responseType: ResponseContentType.Blob})
.map((response) => {
let blob = response.blob();
return {
data: new Blob([blob], {type: 'application/octet-stream'}),
filename: response.headers.get('FILE_NAME')
}
})
.subscribe(res => saveAs(res.data, res.filename))
}

Related

How to read data from the downloaded excel file content from Google drive api in Dart/Flutter?

I am using google drive API to download an excel file in my Flutter app but I want to store the downloaded file content response in a File and then do some update operations using excel dart package, below is the given code from reading an xlsx file from a path location.
var file = "Path_to_pre_existing_Excel_File/excel_file.xlsx"; //here I want to store the response from drive api
var bytes = File(file).readAsBytesSync();
var excel = Excel.decodeBytes(bytes);
//Do some logic here
for (var table in excel.tables.keys) {
print(table); //sheet Name
print(excel.tables[table].maxCols);
print(excel.tables[table].maxRows);
for (var row in excel.tables[table].rows) {
print("$row");
}
}
//then saving the excel file
// updating the excel sheet to Drive
updateToDrive(excel,fileId);
I have created all the required auth functions, drive scopes and my download function looks like this :
Future<void> downloadFile() async{
String fileId = '1TOa4VKfZBHZe######WLA4M95nOWp';
final response = await driveApi.files.get(
fileId,
downloadOptions: drive.DownloadOptions.fullMedia
);
print(response);
}
This function is executing correctely and giving Media type response, but I could not able to read this response so that I could store it in a file.
Any help would be truly appreciated, Thanks
I changed my download function to this, as drive.files.get() was returning a Future Object so I changed it to return Future<Media?> by type casting.
String fileId = "19jF3lOVW563LU6m########jXVLNQ7poXY1Z";
drive.Media? response = (await driveApi.files.get(
fileId,
downloadOptions: drive.DownloadOptions.fullMedia
)) as drive.Media?;
Now response is a Media on which we can listen to the sream to store the response in a file.
To do that first we need to get the app directory by path_provider
final String path = (await getApplicationSupportDirectory()).path;
final String fileName = '$path/Output.xlsx';
File file = File(fileName);
Now we want to write the stream of response Stream<List> into our file object which I found from this link
List<int> dataStore = [];
await response!.stream.listen((data) {
print("DataReceived: ${data.length}");
dataStore.insertAll(dataStore.length, data);
}, onDone: () {
print("Task Done");
file.writeAsBytes(dataStore);
OpenFile.open(file.path);
print("File saved at ${file.path}");
}, onError: (error) {
print("Some Error");
});
Now we can do whatever we want to make changes through excel package.

export to excel from a byte [] in angular

I have the response json from the backend (spring boot):
private byte[] archivoExcel;
private String extension;
private String mime;
What I need in angular is to get this byte [] and export it to excel, for this my answer json in angular is:
export class RespuestaExportar {
archivoExcel: ArrayBuffer;
extension: string;
mime: string;
}
and in my component.ts file I have:
this.reversionesService.getReversionesExportarSeguimiento(this.solicitud).subscribe(res => { this.respuestaExportar=res; let file = new Blob([this.respuestaExportar.archivoExcel], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
var fileURL = URL.createObjectURL(file); window.open(fileURL); }
When I run the 'export' button, it consumes correctly, and it downloads the excel, but this file is damaged. Do I need one more step to solve it? or there is another alternative, since I need to get this byte [] from the backEnd.
I think you can guide yourself from this service that I perform for my backend (SpringBoot) I send a responseObject as json
ObjectResponse response = new ObjectResponse();
ByteArrayInputStream in = getExcel();
byte[] array = new byte[in.available()];
in.read(array);
httpStatus = HttpStatus.OK;
response.setResultado(array);
return new ResponseEntity<>(response, httpStatus);
and I get a Json response with the structure that sent the data is that when serializing it, the response.getResult arrives in base64, that is, already transformed to the client that receives it.
So in my frontend (Angular) I proceed to transform it into a Blob and be able to work the file.
this.subcriber = this.reporteService.generarExcel (). subscribe ((b64Data: any) => {
const byteCharacters = atob (b64Data.result);
const byteNumbers = new Array (byteCharacters.length);
for (let i = 0; i <byteCharacters.length; i ++) {
byteNumbers [i] = byteCharacters.charCodeAt (i);
}
const byteArray = new Uint8Array (byteNumbers);
let blob = new Blob ([byteArray], {type: 'application / vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
const url = window.URL.createObjectURL (blob);
const anchor = document.createElement ('a');
anchor.download = `file.xlsx`;
anchor.href = url;
anchor.click ();
messageService = {
state: false,
messages: null
};
}, (err) => {
this.subcriber.unsubscribe ();
})
I hope it helps you

uploaded files to Azure are corrupted when using dio

I'm trying to upload a file from my phone to azure blob storage as a BlockBlob with a SAS. I can get the file to upload, but it can't be opened once downloaded. The file gets corrupted somehow. I thought this was a content-type problem, but I have tried several different approaches to changing to content-type. Nothing has worked so far.
My code:
FileInfo _fileInfo = await filePicker(); // get the file path and file name
// my getUploadInfo fires a call to my backend to get a SAS.
// I know for a fact that this works because my website uses this SAS to upload files perfectly fine
UploadInfo uploadInfo = await getUploadInfo(_fileInfo.fileName, _fileInfo.filePath);
final bytes = File(_fileInfo.filePath).readAsBytesSync();
try {
final response = await myDio.put(
uploadInfo.url,
data: bytes,
onSendProgress:
(int sent, int total) {
if (total != -1) {
print((sent / total * 100).toStringAsFixed(0) + "%");
}
},
options:
dioPrefix.Options(headers: {
'x-ms-blob-type': 'BlockBlob',
'Content-Type': mime(_fileInfo.filePath),
})
);
} catch (e) {
print(e);
}
This code uploads a file just fine. But I can't open the file since it becomes corrupted. At first, I thought this was a Content-Type problem, so I've tried changing the content type header to: application/octet-stream and multipart/form-data as well. That doesn't work.
I've also tried to do
dioPrefix.FormData formData =
new dioPrefix.FormData.fromMap({
'file': await MultipartFile.fromFile(
_fileInfo.filePath,
filename: _fileInfo.fileName,
)
});
...
final response = await myDio.put(
uploadInfo.url,
data: formData, // This approach is recommended on the dio documentation
onSendProgress:
...
but this also corrupts the file. It gets uploaded, but I can't open it.
I have been able to successfully upload a file with this code, but with this approach I cannot get any type of response so I have no idea whether it uploaded successfully or not (Also, I can't get the progress of the upload):
try {
final data = imageFile.readAsBytesSync();
final response = await http.put( // here, response is empty no matter what i try to print
url,
body: data,
headers: {
'x-ms-blob-type': 'BlockBlob',
'Content-Type': mime(filePath),
});
...
Any help would be greatly appreciated. Thanks
I tried to upload a file using dio in Dart to Azure Blob Storage, and then download and print the content of the file, as the code below.
import 'package:dio/dio.dart';
import 'dart:io';
main() async {
var accountName = '<account name>';
var containerName = '<container name>';
var blobName = '<blob name>';
var sasTokenContainerLevel = '<container level sas token copied from Azure Storage Explorer, such as `st=2019-12-31T07%3A17%3A31Z&se=2020-01-01T07%3A17%3A31Z&sp=racwdl&sv=2018-03-28&sr=c&sig=xxxxxxxxxxxxxxxxxxxxxxxxxx`';
var url = 'https://$accountName.blob.core.windows.net/$containerName/$blobName?$sasTokenContainerLevel';
var data = File(blobName).readAsBytesSync();
var dio = Dio();
try {
final response = await dio.put(
url,
data: data,
onSendProgress:
(int sent, int total) {
if (total != -1) {
print((sent / total * 100).toStringAsFixed(0) + "%");
}
},
options: Options(
headers: {
'x-ms-blob-type': 'BlockBlob',
'Content-Type': 'text/plain',
})
);
print(response.data);
} catch (e) {
print(e);
}
Response response = await dio.get(url);
print(response.data);
}
Then, I ran it and got the result as the figure below.
The content of the uploaded file as blob is the json string encoded from a Uint8List bytes from the funtion readAsBytesSync.
I researched the description and the source code of dio, actually I found dio is only suitable for sending the request body of json format, not for raw content as request body.
Fig 1. The default transformer apply for POST method
Fig 2. https://github.com/flutterchina/dio/blob/master/dio/lib/src/transformer.dart
So to fix it is to write a custom transformer class PutTransformerForRawData instead of the default one to override the function transformRequest, as the code below.
import 'dart:typed_data';
class PutTransformerForRawData extends DefaultTransformer {
#override
Future<String> transformRequest(RequestOptions options) async {
if(options.data is Uint8List) {
return new String.fromCharCodes(options.data);
} else if(options.data is String) {
return options.data;
}
}
}
And to replace the default transformer via the code below.
var dio = Dio();
dio.transformer = PutTransformerForRawData();
Then, you can get the data via the code below.
var data = File(blobName).readAsBytesSync();
Or
var data = File(blobName).readAsStringSync();
Note: the custom transfer PutTransformerForRawData is only for uploading, please remove the download & print code Response response = await dio.get(url); print(response.data);, the default transformer seems to check the response body whether be json format, I got the exception as below when my uploaded file is my sample code.
Unhandled exception:
DioError [DioErrorType.DEFAULT]: FormatException: Unexpected character (at character 1)
import 'dart:typed_data';

FileSaver.js with Angular 7

I have a use case where I need to download data as an excel file. I am using Angular 7 (UI) and Java Spring REST API service. The REST API will generate the Excel file and send it to Angular where I use FileSaver.js module to save the file to disk. I see the blob being created and the size printed on the console, but the file does not save to disk.
This used to work when I had Angular 4. Any help is greatly appreciated.
Also, if anybody can suggest an alternative node module to achieve the same result, I am all ears.
Below is the code snippet in Angular
this.myService.getDataAsXLSX(this.dataList).
subscribe(byteArray => {
this.excelFileAsByteArray = byteArray;
}, error => console.log("There was an error."),
() => {
const blob = new Blob([this.excelFileAsByteArray.body], {type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; ; charset=UTF-8'});
console.log('Blob size in filtered = ' + blob.size);
FileSaver.saveAs(blob, "MyData-" + this.datePipe.transform(this.currentDate, 'MM-dd-yyyy') + ".xlsx"); } );
Here is the code from the REST API
ByteArrayOutputStream outputStream = new ExcelWriter().createExcelWorkbook(dataList, fullName);
byte[] byteArray = outputStream.toByteArray();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Content-Type","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; ; charset=UTF-8");
responseHeaders.set("Content-Disposition", "attachment;filename=MyData.xlsx"); responseHeaders.set("Expires", "0");
return new ResponseEntity(byteArray, responseHeaders, HttpStatus.OK);
Have you set responseType as 'blob' as 'json'. Content type as 'application/vnd.ms-excel'
public getDataAsXLSX(list: Array<any>) {
return this.http.get(this.url, {
headers: this.headers,
responseType: 'blob' as 'json'
});
}

Download an excel file returned from a spring MVC Controller (Ajax call)

I'm developing a web application with spring boot, spring mvc.
I have a controller method that returned an excel file as the following:
in = new FileInputStream(comparsionreport);
out = response.getOutputStream();
response.setHeader("Content-disposition", "attachment; filename=" + comparsionreport.getName());
response.setContentType("application/vnd.ms-excel");
response.addHeader("filename", comparsionreport.getName());
byte[] buffer = new byte[BUFFER_SIZE]; // use bigger if you want
int length = 0;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
In view (javascript), an AJAX call is done like the following:
$.ajax({
url : $('#compareFrm').attr('action'),
type : 'post',
responseType: "arraybuffer",
data : $('#compareFrm').serialize(),
success : function(data, status, xhr) {
// download response file
var jsonData = JSON.stringify(data);
var blob = new Blob(
[ jsonData ],
{
type: "application/vnd.ms-excel"
});
saveAs(blob, xhr.getResponseHeader('filename'));
},
error : function(data) {
}
});
An excel file with the good name is downloaded but when open it, it says that file cannot be open because of wrong extension or format (corrupted file so...)
Anyone have any idea? it's a blocking issue for me :(
Thanks

Resources