Create File object using file path - javascript

With the given file path, create a file object. new File(file_path) doesn't work. (WIN/MAC)
When tried creating a new file object using File constructor. There occurs an error.
new File(decodeURI(file_path))
when the above approach is followed File constructor err comes up.

File API needs a Blob here is work-arround
var GetFileBlobUsingURL = function (url, convertBlob) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.responseType = "blob";
xhr.addEventListener('load', function() {
convertBlob(xhr.response);
});
xhr.send();
};
var blobToFile = function (blob, name) {
blob.lastModifiedDate = new Date();
blob.name = name;
return blob;
};
var GetFileObjectFromURL = function(filePathOrUrl, convertBlob) {
GetFileBlobUsingURL(filePathOrUrl, function (blob) {
convertBlob(blobToFile(blob, 'testFile.jpg'));
});
};
var FileURL="test/test.jpg"
GetFileObjectFromURL(FileURL, function (fileObject) {
console.log(fileObject);
});

Had the exact same issue today, so I'll provide an answer in TypeScript(3.7.5) based on what worked out for me.
Tips:
FileAPI needs a Blob to work with (as others have also stated), it won't work with a file path.
The function:
static async createFile(path: string, name: string, type: string): Promise<File> {
let response = await fetch(path);
let data = await response.blob();
let metadata = {
type: type
};
return new File([data], name, metadata);
}
The call:
await createFile('../assets/images/someInterestingPNGImage.png', 'iAmAFile.png', 'image/png')
.then((file) => {
//do something with ur file.
console.log(file);
});

Here is a simple alternative solution using axios:
const srcToFile = async (src, fileName) => {
const response = await axios.get(src, {
responseType: "blob",
});
const mimeType = response.headers["content-type"];
return new File([response.data], fileName, { type: mimeType });
};

Related

File encryption using Crypto.js with AES cipher algorithm?

I have stacked so far with file uploading program on javascript. The code I have wrapped in if() and else statement.
if(filetype.name.match(/(.*?)\.(jpg|gif|doc|pdf|mp3|mp4|avi|txt|png|jpeg|tif)$/)) {
const formData = new FormData();
var encryptedfile;
const url = 'process.php';
const form = document.querySelector('form');
form.addEventListener('submit', (e) => {
e.preventDefault();
const file = document.querySelector('[type=file]').files[0];
var reader = new FileReader();
And after that, I have written reader.onload function. The code out of that function doesn't see what inside, because of local variables. But the problem not about that
reader.onload = function() {
var encrypted = CryptoJS.AES.encrypt(reader.result, "ThisIsPasswordForTheFile" );
encryptedfile = new File([encrypted], "foo.txt", {
type: "text/plain"
});
formData.append('files',encryptedfile);
}
reader.readAsDataURL(file);
The problem is const formData = new FormData() doesn't add this encrypted file
after I send it to the server, and it is successful, but no file the upload folder
fetch(url, {
method: 'POST',
body: formData,
}).then((response) => {
console.log("ok");
// document.location.href = _url+"process.php";
});
});
}
And I am not really sure about CryptoJS.AES.encrypt code. is this code encrypt right file from document input? const file = document.querySelector('[type=file]').files[0];
Please take a look at my whole js code
document.querySelector("#inputGroupFile01").addEventListener("change", function(){
const reader = new FileReader();
var filetype =this.files[0]
if(filetype.name.match(/(.*?)\.(jpg|gif|doc|pdf|mp3|mp4|avi|txt|png|jpeg|tif)$/)) {
const formData = new FormData();
var encryptedfile;
const url = 'process.php';
const form = document.querySelector('form');
form.addEventListener('submit', (e) => {
e.preventDefault();
const file = document.querySelector('[type=file]').files[0];
var reader = new FileReader();
reader.onload = function() {
var encrypted = CryptoJS.AES.encrypt(reader.result, "ThisIsPasswordForTheFile" );
encryptedfile = new File([encrypted], "foo.txt", {
type: "text/plain"
});
formData.append('files',encryptedfile);
}
reader.readAsDataURL(file);
fetch(url, {
method: 'POST',
body: formData,
}).then((response) => {
console.log("ok");
});
});
}
else{
alert("please choose another one!");
}
});

Convert File path to File Object in JavaScript

I want to upload file on firebase using JavaScript my file on my server and also have file path but firebase doesn't allow to upload file from file path and it shows the following error message:
code_: "storage/invalid-argument"
message_: "Firebase Storage: Invalid argument in `put` at index 0: Expected Blob or File."
name_: "FirebaseError"
serverResponse_: null
How can I upload the file or convert the file path to file object?
You can use this function to upload file in firebase. the uri is the file path which further creates the file type required in firebase.
const uploadImage = async (uri) => {
const uniqid = () => Math.random().toString(36).substr(2, 9);
const ext = uri.split('.').pop(); // Extract image extension
const filename = `${uniqid()}.${ext}`; // Generate unique name
const ref = fb.storage().ref().child(`images/${filename}`);
const response = await fetch(uri);
// const blob = await response.blob();
const blob = await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onload = function () {
resolve(xhr.response);
};
xhr.onerror = function (e) {
console.log(e);
reject(new TypeError('Network request failed'));
};
xhr.responseType = 'blob';
xhr.open('GET', uri, true);
xhr.send(null);
});
const snapshot = await ref.put(blob);
blob.close();
const imgUrl = await snapshot.ref.getDownloadURL();
return imgUrl;
}
after this function you can call this function to upload your Image in firebase database like this,
const formSubmit= async(values, action)=>{
console.log(values);
try{
uploadImage(image) //this is my image upload code
.then( async(res) => {
setLoader(false);
var user = fb.auth().currentUser;
await user.updateProfile({
displayName: values.name,
photoURL: res // use the response url to store in mapped data table
}).then(function(res) {
console.log(res)
}).catch(function(error) {
console.log(error)
});
})
.catch(error => {
console.log('it does not work')
console.error(error)
})
}catch(error){
console.log(error)
}
}

Convert S3 byte array to base64

On my NodeJS server I download an image that I need to embed in an email. My bucket is NOT public so just using the link will not work, as is not the solution I want for the requirements of either this question or the project.
I'm using a HTML email for this with something like:
<p>Embedded image: <img src="data:image/jpeg;charset=utf-8;base64,{{url}}" /></p>
So I download from S3
s3.getObject(
{ Bucket: "mybucket", Key: "mykey" },
function (error, data) {
if (error != null) {
console.log("Errror" + error)
} else {
console.log("Loaded " + data.ContentLength + " bytes")
and then I'm trying to convert data.body to UTF-8 base 64
I thought something like
"data:image/png;base64," + new String(encoder.encode(data.body), "UTF-8")
But it doesn't seem to work, and I'm struggling to define encoder to be able to do this.
Make sure you are getting image data in data.Body. I think data.Body already a buffer and you can construct src URL as bellow:
// Convert Body from a Buffer to a String
let base64String= data.Body.toString('base64');
let src = "data:image/jpeg;base64,"+base64String;
if you store s3 bucket object in binary format, to received it and convert it to base64 can be done:
const blobToBase64 = (blob) => {
const reader = new FileReader()
reader.readAsDataURL(blob)
return new Promise((rs, rj) => {
reader.onloadend = () => {
rs(reader.result)
}
reader.onerror = rj
})
}
function useImage({s3Link}) {
const [src, setSrc] = React.useState(null)
React.useEffect(() => {
async function query({link}) {
//link is link to s3 bucket URL link e.g
// const link = s3.getSignedUrl('getObject', {
// Bucket: bucketnamw,
// Key: key,
// Expires: 30,
// })
const r = await fetch(link)
const blob = await r.blob()
const base64 = await blobToBase64(blob)
console.log(`base64!`, base64)
setSrc(base64)
}
if (s3Link) {
query({link: s3Link})
}
}, [s3Link, setSrc])
return [{src}]
}

How to read Blob (octet-stream) to JSON object?

From a http request, a blob (b) (type application/octet-stream) is downloaded and then needs to be processed, it contains a json object.
I tried the following:
var reader = new FileReader();
reader.readAsText(b);
var readResult = <string> reader.result;
console.log(readResult);
var obj = JSON.parse(readResult);
It doesn´t work, and readResult is null.
How can you process a blob that contains a json into a json object?
When the request has responseType: 'blob' it returns binary, but if an error occurs message is returned as JSON inside Blob.
This is how you can decode JSON messages from blob:
(response) => {
return response;
},
async (error) => {
if (error.response.data instanceof Blob) {
const blob = new Blob([error.response.data]);
const data = await blob.text();
const { message, details } = JSON.parse(data);
//display message and details to the user
}
}
You will need an onload event like so:
var blob = new Blob([JSON.stringify({"test": "Hello from JSON!"})], {type : "application/json"}),
reader = new FileReader();
reader.onload = function() {
document.body.innerText = JSON.parse(this.result).test;
};
reader.readAsText(blob);
Code example :
try{
await this.exampleservice.MygetRequest().then(httpResponse => {
httpResponse.body.text().then(text => {
//console.log(text);
obj = JSON.parse(text);
//console.log('obj after parse:' ,obj);
let data = obj.result.data;
console.log('My data:' ,data);
});
});
}catch (error) {
if (error instanceof HttpErrorResponse) {
Swal({
type: 'error',
title: this.errorsService.getErrorMessage(error.status),
showConfirmButton: false,
timer: 1500
});
this.feedback_errors = error.error.errors;
this.feedback_errors_keys = this.feedback_errors ? Object.keys(this.feedback_errors) : null;
}
}

How do I post an image as xhr / ajax request? (fails in iOS 10.0.2+)

Uploading an image (from a dataUrl format) using an XHR request appears to fail on iOS (10.0.2+), other browsers are working: the latest Chrome (54.0.2840.71), Firefox (50.0).
The request seems to be constructed correctly in JS, but there is no actual data attached to the request.
var dataUrl = "data:image/jpeg;base64,/9j/4AAQSkZJR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JOGPBbBYbuVUk9z6jAxS+0zrK0aNtVngQ7QB8rlVYccjIAFaEgEFyiQ/KDHAp75DNJnP1wKrmsQ43Zm6iYUiW4Z8bBui4AKOcc8H5hyQeMdfpVIa5dwKAEEkcpJk5AWPHPRm2jnAH1B561s3lvD5roVyEGRnk5wDkk8k5JIJziuE1K6nXSb9A/yx+WVBAIB2M2ee+QPyHoKzcupfKrEfiOdm1O1jndcOSHck7COSTkE5APzYAxjHBAIb86/22Pgt/b/h6y+KGkW8TXekW/2a/iYTMDYyOT56+Wc4tndnbK7fLZ2ZmCAH7r8TSynVbeNnYqFL7SSRuUKQcH3J/MjpWtqaLc2xtpxvikiIdD0YAZ5rTDYl4etGrFbfiE6KqQcGfzW29tp+oWUemazqE1pb6gkBF3HLHdsJLSERLDJD58aKhO4RPIUx8h3BAzDiraa30m5gnsI723hliW3uHjnH75WA89eEC7HVgvlsGAz8+7v7d4/8N6JZ+NvijaWtqsUOhtdNYIrMBCU1e1tgevzfuZGX5t3Xd97Brz74rpGviyd0RFa4htriUqoXfLNGpkY4A5Y8n3JPUkn7uo+WMatNJWtbTV72vbTTl9bO13ZHzVSrKdb2dRt2v187u3q5X9bvqdXffFixu/It9bsdF1J7GL7Lbz67Hdz332ZHZo43aFdv7oN5fOCSpbHzAmh/wsrwt/0AvBv/AIC6h/8AE1y2rst09nNcxxSyGwsss8SMx/cJ3K5rJ8m2/wCfe3/78R//ABNZ/wCvWKWjgvw/yPtH4c0W7wq6dNH/AJn/2Q==";
// create base64 encoded version of image
let parts = dataUrl.split(',');
let mime = parts[0].match(/:(.*?);/)[1];
let bstr = atob(parts[1]);
let n = bstr.length;
let u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
let blob = new window.Blob([u8arr], {type: mime});
file = new window.File([blob], file.name);
// create formdata for uploading
let data = new FormData();
data.append('file', file, file.name);
// send the post request
var xhr = new XMLHttpRequest;
xhr.onload = function (oEvent) {
// Uploaded.
if ( oEvent.target.status == '200') {
console.log('upload with XHR success');
} else {
console.log('upload with XHR error');
}
};
xhr.open("POST", '/upload-endpoint', false);
xhr.send(data);
The main idea of how to upload an image to a RESTful api is using multipart/form-data.
If you use Swift 3.0, you can try these code below, using URLSession:
import Cocoa
fileprivate extension ImageUploader {
static let urlString = "/upload-endpoint"
static let boundary = "---------------------------14737809831460499985746342449"
static let contentType = "multipart/form-data; boundary=\(boundary)"
}
typealias CompletionBlock = (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Swift.Void
class ImageUploader {
func recognize(with paths: [String], completionBlock: #escaping CompletionBlock) {
guard paths.count > 0 else { return }
let path = paths.first!
guard let image = NSImage(contentsOfFile: paths.first!) else { return }
let imgRep: NSBitmapImageRep = image.representations.first as! NSBitmapImageRep
let data = imgRep.representation(using: .JPEG, properties: [:])
var request = URLRequest(url: URL(string: ImageUploader.urlString)!)
request.httpMethod = "POST"
request.timeoutInterval = 10
request.addValue(ImageUploader.contentType, forHTTPHeaderField: "Content-Type")
var postbody = Data()
postbody.append("\r\n--\(ImageUploader.boundary)\r\n".data(using: .utf8)!)
postbody.append("Content-Disposition: form-data; name=\"file\"; filename=\"file\"\r\n".data(using: .utf8)!)
postbody.append("Content-Type: application/octet-stream\r\n\r\n".data(using: .utf8)!)
postbody.append(data!)
postbody.append("\r\n--\(ImageUploader.boundary)\r\n".data(using: .utf8)!)
request.httpBody = postbody
let task = URLSession.shared.dataTask(with: request) { data, response, error in
completionBlock(data, response, error)
}
task.resume()
}
}
Maybe try using fetch instead of xhr?
let dataUrl = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
let {name} = file
// First convert base64 to blob
fetch(dataUrl)
.then(res => res.blob())
.then(blob => {
// Now upload the blob
let fd = new FormData()
fd.append('file', blob, name)
fetch('/upload-endpoint', {method: 'POST', body: fd})
.then(res => {
console.log('upload with fetch success:', res.ok)
})
})

Categories

Resources