How can I upload files in expressjs with multer? - express

I am trying to make a restaurant system and I am trying to make a food adding system.I use expressjs for server-side and for file uploads I use multer.I am trying to do it but it does not work.What is the problem ?
app.post("/Add-food-complete",function(req,res){
var food_name=req.body.add_food_food_name_input;
var food_price=req.body.add_food_food_price_input;
var food_type=req.body.Food_type_selection;
console.log(food_price);
console.log(food_type);
var upload=multer({dest:"Food-Photos/"});
upload.single('add_food_food_photo_input');//This is the name of the file input

Related

way to use 'lowdb' in 'express'

Recently, I am trying to make a web page using express, and I am trying to manage login session information and bulletin board data through lowdb.
But when I try to generate and run the code that requires lowdb and controls lowdb,
There is a error with the require syntax with the err_code 'ERR_REQUIRE_ESM'.
When I googled it, it said that it was a syntax compatibility problem between commonjs and ESM, so do I have to convert all other codes to ESM because of one lowdb?
The code below is the code I made to control lowdb.
var low = require('lowdb');
var FileSync = require('lowdb/adapters/FileSync');
var adapter = new FileSync('db.json');
var db = low(adapter);
db.defaults({users:[], topics:[]}).write();
module.exports = db;
I don't want to convert the whole code to ESM because of lowdb, I want to use lowdb through commonjs. Or is there any other good way to handle local db via json file like lowdb?
Use lowdb#1.0.0 and it will work.
npm i lowdb#1.0.0

Why FileExtensionContentTypeProvider no work with .min.js extension?

I have some css&js files in my project and I used the BuildBundlerMinifier NuGet package to minify and obfuscate them.
For example, the app.js will minify and obfuscate into app.min.js in the same directory.
Now I want the user can access the app.min.js but can't access the app.js.
I do this for I don't want anybody else to access the source code of my js.
Although someone still can get its source code from the app.min.js while I don't want them to get it easily.
I tried to use FileExtensionContentTypeProvider in Configure of startup.cs to achieve this:
var provider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider();
provider.Mappings.Remove(".js");
provider.Mappings.Add(new KeyValuePair<string, string>(".min.js", "application/javascript"));
app.UseStaticFiles(new StaticFileOptions()
{
ContentTypeProvider=provider
});
However, after it runs I can access neither app.js nor app.min.js.
What's wrong with my code?
Thank you.
The FileExtensionContentTypeProvider is only meant to provide a mapping from file extension to the correct MIME type. In order to retrieve the file extension from a file name, it will do the following:
private static string? GetExtension(string path)
{
int index = path.LastIndexOf('.');
if (index < 0)
return null;
return path.Substring(index);
}
It will take the very last part of the extension. So with app.min.js, the reported file extension will still be .js and not .min.js, and as such the mapping for .js will be required.
Modifying the MIME type mapping in order to disallow certain file extensions is probably not the best strategy. It would be better to modify the underlying file provider itself to handle that.
Alternatively, if you want to prevent access to non-minified JavaScript files, you could also split the middleware to conditionally prevent serving static files for any request to a path that ends with .js that is not a .min.js:
app.UseWhen(ctx => !ctx.Request.Path.HasValue
|| !ctx.Request.Path.Value.EndsWith(".js")
|| ctx.Request.Path.Value.EndsWith(".min.js"), app2 =>
{
app2.UseStaticFiles();
});

MVC4 - How to upload a file partially (only the first 10 lines, for e.g.)

ASP.NET MVC - Is it possible to upload only the first 10 lines of a file? Basically, we have some files that can range from 1-10GB but the data that we need is present only in the first 10 rows in the file. Using the typical web development approache, we'd upload the whole file to the server and then read the first 10 rows, but uploading a 10GB file just to read a few bytes of data seems a big waste of resources. Is it possible to read such a file without uploading all of it to the webserver?
Solution - FileAPIs slice function solved this problem (thanks to Chris below). The simplified code is below for anyone interested -
var sampleFile = document.getElementById('yourfileelement').files[0];
var reader = new FileReader();
var fileData = sampleFile.slice(0, 500000); //Read top 500000 bytes
reader.onprogress = function (evt) { //Show progressbar etc }
reader.onloadend = function (evt) { alert(evt.target.result); } //evt.target.result contains the file data that was read
reader.readAsText(fileClientReadData);
No, but you may be able to accomplish it using the File API client-side to read and send to the server via AJAX just the first 10 lines. However, note that the File API is only supported in modern browsers, so this won't work with IE 9 or less. You might be able to create a more comprehensive solution using a Flash or Java applet, but ugh.

Download files with extension .txt or wildcard from Amazon S3 using .net sdk?

Is there a way to download all the files with specific extension (e.g. *.txt) OR wildcard string (1234 *.*) from a folder from Amazon S3 using .Net SDK in one go rather then looping through the list of files.
You can't make one call to download all files of a certain pattern but you can use the pattern in the list object call and then download the individual files. Using the S3DirectoryInfo from the Amazon.S3.IO namespace can simplify this.
S3DirectoryInfo info = new S3DirectoryInfo(s3Client, bucketName);
foreach (var file in info.GetFiles("*.jpg", SearchOption.AllDirectories))
{
var localPath = Path.Combine(#"C:\Temp\download", file.FullName.Substring(bucketName.Length + 2));
Console.WriteLine("Downloading: {0}", localPath);
file.CopyToLocal(localPath);
}

How to load files dynamically in an AIR app? Do I have to use the File class?

I have an XML file that specifies the image files that I need to load. The XML file and the image files live in a subfolder relative to where the AIR app lives.
I need to load the XML file and also the images (load them and add them as children to a movieclip)
In my AIR app, when I tried to load it via the URLRequest, it didn't work.
myLoader = new URLLoader();
myLoader.load(new URLRequest(xmlFilename));
myLoader.addEventListener(Event.COMPLETE, processXML);
(I know this works from a .swf b/c I've tested it)
I've found some sample which uses the File class and here's my code and this does work:
var aFile:File = File.applicationDirectory.resolvePath( xmlFilename );
var aStream:FileStream = new FileStream();
aStream.open( aFile, FileMode.READ );
configXML = new XML( aStream.readUTFBytes( aStream.bytesAvailable ) );
aStream.close();
processXML();
...
I'm now trying to load the images specified in the XML file and I'm finding that I have to use the File class to reference the image in the file system.
var engImageFile:File = File.applicationDirectory.resolvePath( "./english/"+ engFilename );
ldr = new Loader();
var urlReq:URLRequest = new URLRequest( engImageFile.url );
ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, engImgLoaded );
ldr.load(urlReq);
Is this the way that AIR accesses files (using the File class) when it wants to read/load/etc. them?
if I understand your question correctly, I belive you just need to load it as a data source/array, which you can then bind to a component of your choosing.
In the documentation it states that if you use the File class to access files then this is safe across all platforms. Also the .url property of the File object will use the appropriate URL scheme...
"app:" - relative to the application directory
"app-storage:" - relative to the special application storage directory
"file:" - all else...I think if you specify an absolute path