i'm trying to download file using node.js and this is my code:
var file_url = req.body.filename;
var DOWNLOAD_DIR = './downloads/';
var options = {
host: url.parse(file_url).host,
port: 80,
path: url.parse(file_url).pathname
};
var file_name = url.parse(file_url).pathname.split('/').pop();
var file = fs.createWriteStream(DOWNLOAD_DIR + file_name);
http.get(options, function (resp) {
resp.on('data', function (data) {
file.write(data);
}).on('end', function () {
file.end();
console.log(file_name + ' downloaded to ' + DOWNLOAD_DIR);
delet(file_url);
});
});
here i'm giving DOWNLOAD_DIR manually so it is downloading to that
directory as it is localhost not a problem, but when i'm uploading this
code into server then the file should be downloaded to that particular
user's machine, so how to give path dynamically to DOWNLOAD_DIR variable
if the Downloads folder is in home directory (and you want to download in Downloads folder), then you can use
var path = require('path');
var file_url = req.body.filename;
var DOWNLOAD_DIR = path.join(process.env.HOME || process.env.USERPROFILE, 'downloads/');
var file_name = url.parse(file_url).pathname.split('/').pop();
var file_path = path.join(DOWNLOAD_DIR,file_name);
var file = fs.createWriteStream(file_path);
PS: While dealing with paths and files, always use path module of nodejs to generate path.
Related
I have the below working on local host. However when I publish to azure the connection to my network doesn't work? I guess, this is obvious - my web app doesn't have connection to my local network. My question is, is there a way to allow a connection?
try
{
var path = "//192.168.49.14/Data/18 Customer Requests/Customer Request System/Request Letters/";
//Fetch all files in the Folder (Directory).
string[] filePaths = Directory.GetFiles(Path.Combine(path));
//Copy File names to Model collection.
var localFileList = new List<FileData>();
var assocFiles = new List<FileData>();
var linkedFiles = new List<FileData>();
assocFiles = localFileList.Where(x => x.FileName.Contains(id.ToString())).ToList();
foreach (var filePath in filePaths)
{
localFileList.Add(new FileData { FileName = Path.GetFileName(filePath) });
}
assocFiles = localFileList.Where(x => x.FileName.Contains(id.ToString())).ToList();
foreach (var item in assocFiles)
{
linkedFiles.Add(new FileData { FileName = Path.GetFileName(item.FileName) });
}
ViewBag.localFiles = linkedFiles;
}
catch
{
;
}
return View(Complaint);
I tried check file for exists but on all case flutter can't found file from directory.
Code:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final appDocDir = await path_provider.getApplicationDocumentsDirectory();
var path1 = '/assets/images/text.txt';
var path2 = 'assets/images/text.txt';
var path3 = '${appDocDir.path}/assets/images/banner.png';
if (await File(path3).exists()) {
print("File exists");
} else {
print("File don't exists");
}
}
File pubspec.yaml:
flutter:
assets:
- assets/images/
- assets/icons/
Where I have error on my code?
Assets directory should be in project root path.
assets
lib
build
etc.
You should to write file names too:
text.txt, banner.png
flutter:
assets:
- assets/images/text.txt
- assets/images/banner.png
-
path3:
var path3 = '${appDocDir.path}/assets/images/banner.png';
You don't need to use path_provider to get image form assets dir.
I am trying to write a script to get the URL of a specific yet dynamic Google Document in Drive. The Google Document is created every Tuesday with its date at the end of the title of the Google Document.
"TypeError: Cannot find function getUrl in object FileIterator." error shows on line var agendaURL = file.getUrl();. Not sure how to debug this.
var ssUrl = 'LINK BUFFERED';
var sheetName = 'SHEET1'; // name of sheet to use
var rangeName = 'C30'; // range of values to include
var dateRange = SpreadsheetApp.openByUrl(ssUrl)
.getSheetByName(sheetName)
.getRange(rangeName)
.getValues();
// NEED TO find how to find file by name below!
var file = DriveApp.getFilesByName("Weekly Agenda | " +dateRange);
var agendaURL = file.getUrl();
It is because you need to iterate through all of the files that meet the search criteria. The solution is here: https://developers.google.com/apps-script/reference/drive/drive-app
var files = DriveApp.getFilesByName("Weekly Agenda | " +dateRange);
while (files.hasNext()) {
var file = files.next();
var agendaURL = file.getUrl();
return agendaURL //for first one, or you can return each one, push to an array, etc...
}
The value which got by DriveApp.getFilesByName() is FileIterator. So it is necessary to retrieve the filename from the value as follows.
var filename = "Weekly Agenda | " + dateRange;
var file = DriveApp.getFilesByName(filename);
while (file.hasNext()) {
var file = files.next();
if (file.getName() == filename) {
var agendaURL = file.getUrl();
}
}
I'm working with mongoose-thumbnail to get an thumbnail of a image I upload, I done it exactly as in the github mongoose-thumbnail example and it works very well, below is my code
var mongoose = require('mongoose');
var thumbnailPluginLib = require('mongoose-thumbnail');
var path = require('path');
var thumbnailPlugin = thumbnailPluginLib.thumbnailPlugin;
var make_upload_to_model = thumbnailPluginLib.make_upload_to_model;
var uploads_base = path.join(__dirname, "uploads");
var uploads = path.join(uploads_base, "u");
var Schema = mongoose.Schema;
var ApplicationSchema = new Schema({
appName: String,
appTempPath:String,
userId : String,
status : String,
templateId: String,
appSettings:[{header: String, color: String, font: String, fontSize: Number, backImg: String}]
});
ApplicationSchema.plugin(thumbnailPlugin, {
name: "appIcon",
inline: true,
upload_to: make_upload_to_model(uploads, 'appIcon')
});
var Application = mongoose.model('Application', ApplicationSchema);
My problem is how do I find the "uploads" and "u" directories?? I can't find it anywhere in my directory, Please help.
It should be in the script_current_directory/uploads/u but it's not?
What does console.log(uploads) say? How about console.log(make_upload_to_model(uploads, 'appIcon'))
I'm not sure if your library creates directories that don't exist.
If all else fails, you can change the uploads directory or name to something arbitrary like XYZXYZXYZ and do a find . -name "XYZXYZXYZ" and it should turn up.
"the __dirname` keyword contains the path to the root directory of the currently executing script."
I'm currently storing images within the webapp folder of my Lift project, which I know will cause problems in future.
val path = "src/main/webapp/files/"
And the code I'm using to save it:
case Full(file) =>
val holder = new File(path, "test.txt")
val output = new FileOutputStream(holder)
try {
output.write(file)
} finally {
output.close()
}
}
What I'm trying to do is save the to the server root in an easily manageable folder called files, so SERVER_ROOT/files outside of the project folder.
Firstly how would I access the path to the root of the server so I can save them there?
Secondly how would I serve these files from my app, so I can display them on a page?
Thanks in advance, any help much appreciated :)
You have to store file to exact place on filesystem according to absolute path. I have written this code and it works, so maybe it helps you:
def storeFile (file : FileParamHolder): Box[File] =
{
getBaseApplicationPath match
{
case Full(appBasePath) =>
{
var uploadDir = new File(appBasePath + "RELATIVE PATH TO YOUR UPLOAD DIR")
val uploadingFile = new File(uploadDir, file.fileName)
println("upload file to: " + uploadingFile.getAbsolutePath)
var output = new FileOutputStream(uploadingFile)
try
{
output.write(file.file)
}
catch
{
case e => println(e)
}
finally
{
output.close
output = null
}
Full(uploadingFile)
}
case _ => Empty
}
}
and this is my getBaseApplicationPath function which finds out absolute path of local machine (server or your devel PC):
def getBaseApplicationPath: Box[String] =
{
LiftRules.context match
{
case context: HTTPServletContext =>
{
var baseApp: String = context.ctx.getRealPath("/")
if(!baseApp.endsWith(File.separator))
baseApp = baseApp + File.separator
Full(baseApp)
}
case _ => Empty
}
}