Uploaded file's filepath node - javascript

I uploaded a file in node using the
Input type:"file"
The object generated by the Input field is as follows
{
lastModified: 1524895591000
lastModifiedDate: Date 2018-04-28T06:06:31.000Z
name: "images.jpeg"
size: 10732
type: "image/jpeg"
webkitRelativePath: ""
}
How do I get the full path to the file in node? I am on a ubuntu 17.10
Update:
I basically need the full path for the file FileToBeUploaded.txt, Which will be something like
/home/m/Desktop/FileToBeUploaded.txt
So I can execute a child process command on the file Path of the uploaded file.

Related

How to to get Image file metadata from it's path

I want to upload a image file from Reactjs which is in my project folder ("../assets.cover.png") without using input, when I'm trying import cover from "../assets.cover.png" it's giving me file path but what I need is file metadata to upload it.
Basically my final intention is to upload that image file and calculate user upload speed.
I was able to resolve it after reading after reading this documentation
import cover from "../assets/cover.png"; // importing img file
let blob = await fetch(cover).then((r) => r.blob()); //creating blob object
const file = new File([blob], "cover.png", {
type: "image/png",
});
console.log(file);
// output
// {
// lastModified: 1656486792733
// lastModifiedDate: Wed Jun 29 2022 12:43:12 GMT+0530 (India Standard Time) {}
// name: "cover.png"
// size: 1446458
// type: "image/png"
// webkitRelativePath: ""
// }
The imported image is just a file path relative to the base url for use on properties, you'll have to read the file to do what you are attempting.

node.js unzip file. file name is broken

I tried to unzip the file.
let zip = new AdmZip('./sample.zip')
zip.extractAllTo(path, true)
but,the name of the contents file is Japanese.
So file name is broken.
���̑�-�T�|�[�g�O
Any suggestions?

File object to image with correct file name instead of src="blob..."

I have a File object myFile that looks like this in console:
File {
name: "myimage.jpg",
lastModified: 1465476925001,
lastModifiedDate: Thu Jun 09 2016 14:55:25 GMT+0200 (CEST),
size: 33002
type: "image/jpeg"
}
But when i create an image out of it with
var image = new Image();
image.src = URL.createObjectURL(myFile);
I get:
<img src="blob:http://myurl.com/6b2b83d8-ac36-40c1-8ab1-c4f07b019ba5">
When i try to save the file with right-click, the file-name is empty or "6b2b83d8-ac36-40c1-8ab1-c4f07b019ba5" instead of "myimage.jpg". The file-name from the file-object is gone. Is there any way to set the image file name?
The Problem
The File object is sort of an extended version of a Blob that allows it to hold metadata such as filename, size etc.
However, while createObjectURL() will reference both File and Blob the data read through the blob: protocol will only consist of the binary file data itself as when loading via other protocols. No metadata (such as filename) will be provided via the protocol.
Other examples where filename is not considered could be when an image is loaded via for example a PHP or ASPX page (/getimage.aspx?id=1). Also here there is no metadata provided and browser would suggest something like "getimage.aspx%3Fid=1" as filename in this case. As expected.
The IMG tag itself never assumes any filename even if one is used at source point; it only holds what is given to it via the src attribute/property as-is, as a connection point to retrieve the needed binary data for decoding.
In this case the source point is blob:*/* which then will be what IMG tag references via src, and is what the browser use when the data is to be saved.
Workarounds
The only way to hold on to a filename from the File object is to keep track of it so you have access to it when needed for download.
But also this is limited as you can only specify a filename using the download attribute in an A tag. And of course, some browsers such as <= IE11 does not support this attribute.
<img ..>
In IE10+ there is the proprietary method msSaveBlob() which can be used instead though:
window.navigator.msSaveBlob(myBlob, 'filename.jpg');
Besides from these there are no common way to specify a default filename from within the client.
Example fiddle

Can I get upload path at runtime?

I need to get upload path of my uploaded files via jquery-file-uploader.
$('.new_plotphoto').fileupload({
done: function (e, data) {
var filess = data.files[0];
var filename = filess.name;
var filepath = filess.filepath;
console.log(filename); // this shows filename
console.log(filepath); // this shows undefined
}
});
Path of uploading file is a secure feature. From W3C File API docs about file name:
The name of the file; on getting, this must return the name of the
file as a string. There are numerous file name variations on different
systems; this is merely the name of the file, without path
information.
File path will be enabled only then you upload directory:
<input type=file multiple directory webkitdirectory onchange="console.log(this.files)">
In this case webkitRelativePath(for WebKit browsers) field of file object will be not empty. But it will show path only inside uploaded directory.
Also note that iOS browsers then uploading images always have this file name: image.jpg and no info about path.

How to select Default folder location in File Upload Control?

I want to set the default path folder location has to be opened, Once I press the "browse" button in File Control. Please Help me.
This forum says it can't be done.
You are wanting to control the directory location that the browse
starts in and not the save path the file is uploaded to, correct?
I could be wrong but since the server never knows the file structure
of the client machine, the developers of that control probably did not
provide for that functionality.
COPIED: How to set physical path to upload a file in Asp.Net?
To use a folder outside of the application:
//check to make sure a file is selected
if (FileUpload1.HasFile)
{
//create the path to save the file to
string fileName = Path.Combine(#"E:\Project\Folders", FileUpload1.FileName);
//save the file to our local path
FileUpload1.SaveAs(fileName);
}
Of course, you wouldn't hardcode the path in a production application but this should save the file using the absolute path you described.
With regards to locating the file once you have saved it (per comments):
if (FileUpload1.HasFile)
{
string fileName = Path.Combine(#"E:\Project\Folders", FileUpload1.FileName);
FileUpload1.SaveAs(fileName);
FileInfo fileToDownload = new FileInfo( filename );
if (fileToDownload.Exists){
Process.Start(fileToDownload.FullName);
}
else {
MessageBox("File Not Saved!");
return;
}
}

Categories

Resources