admin管理员组文章数量:1291721
I am currently using Angular on the Front-End and I need to upload a file using form and convert it to blob before sending the file to server.
What I actually want to do: I want upload a file from local system and then convert it to blob after doing some validations.
This is my HTML
<input type="file" (change)="changeFile($event)">
This is my method
changeFile(event) {
console.log(event);
var oReq = new XMLHttpRequest();
oReq.open("GET", `/${event.target.files[0].name}`, true);
oReq.responseType = "blob";
oReq.onload = function(oEvent) {
var blob = oReq.response;
console.log(blob)
};
oReq.send();
}
It does convert it to blob but show an error as I am trying to make a get request when actually I am uploading file from my local system. It would be great if anyone can share relevant info or blog or whatever's helpful in order to achieve this.
I am currently using Angular on the Front-End and I need to upload a file using form and convert it to blob before sending the file to server.
What I actually want to do: I want upload a file from local system and then convert it to blob after doing some validations.
This is my HTML
<input type="file" (change)="changeFile($event)">
This is my method
changeFile(event) {
console.log(event);
var oReq = new XMLHttpRequest();
oReq.open("GET", `/${event.target.files[0].name}`, true);
oReq.responseType = "blob";
oReq.onload = function(oEvent) {
var blob = oReq.response;
console.log(blob)
};
oReq.send();
}
It does convert it to blob but show an error as I am trying to make a get request when actually I am uploading file from my local system. It would be great if anyone can share relevant info or blog or whatever's helpful in order to achieve this.
Share Improve this question asked Jan 24, 2019 at 7:27 Immad HamidImmad Hamid 7892 gold badges8 silver badges21 bronze badges2 Answers
Reset to default 5Blob is almost a File with two less properties. In your case what you want to achieve can be achieved by this approach that I have mentioned below:
You can convert the file to base 64 and then to blob and in order to show it, you can convert it back to base64, like this:
HTML:
<input type="file" (change)="uploadFile($event)">
TS file:
changeFile(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
}
uploadFile(event) {
if (event.target.value) {
const file = event.target.files[0];
const type = file.type;
this.changeFile(file).then((base64: string): any => {
console.log(base64);
this.fileBlob = this.b64Blob([base64], type);
console.log(this.fileBlob)
});
} else alert('Nothing')
}
Cheers! Happy Coding....
Just simply use to convert local file selected by user into blob
new Blob([fileSelectedFromLocalByUser])
changeFile($event: Event) {
const files = ($event.target as HTMLInputElement).files;
if(files) {
const file = files[0];
this.fileBlob = new Blob([file]);
}
}
本文标签: javascriptAngular Upload ExcelPdf file and convert it into BLOB before PostStack Overflow
版权声明:本文标题:javascript - Angular: Upload ExcelPdf file and convert it into BLOB before Post - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741537519a2384115.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论