### Picture upload 1. Front-end perspective a. Send the picture to the back-end ajax1. The front-end gets the picture information File field 2. Save the file information to formdata3. Call the back-end The written api interface sends data b. accepts the returned data The front-end page displays the picture 2. The purpose of the back-end perspective: the picture uploaded by the front-end 1. The picture itself should be accessible (static resource directory) a. Obtain the data uploaded by the picture (multer ().singer('hehe') req.file) b. Save the data to the file and go to fs.writeFile('path', req.file.buffer) The file name is not repeated (timestamp + random water) suffix name and source The file is consistent (minitype) The uploaded file size cannot exceed a certain size (size) The absolute path path.join(__dirname,'./www') is used to write the path. 2. The path information is stored in the data
. 3. Precautions 1. Data type formdata2. method post3. normal ajaxpost data format form json Specific code: server server.js const express = require('express')let app = express()const multer = require('multer')const fs = require ('fs')const path = require('path')//single is a single image upload, multi-image upload array, single is the key value of the uploaded image//The image is related to req.fileapp.use('/ public',express.static(path.join(__dirname,'./www')))
app.post('/aa',multer().single('img'),(req,res)=>{ let {buffer,mimetype} = req.file; let fileName = (new Date()).getTime () + parseInt(Math.random()*3435) + parseInt(Math.random()*6575); let fileType = mimetype.split('/')[1]; let filePath = path.join(__dirname,' /www/images') let apath = `http://localhost:5500/public/images/${fileName}.${fileType}` fs.writeFile(`./www/images/${fileName}.${ fileType}`,buffer,(data)=>{ if(data){ res.send({err:0,msg:"Upload failed"}) }else{ res.send({err:1,msg:"Upload Success",imgPath:apath}) } })})
app.listen('5500',()=>{ console.log('start')}) Front end:upload image

nodejs Ideas for uploading front-end and back-end pictures Related posts

  1. Algorithm Design Ideas (1) - Exhaustive Method

    This article is the course notes of Teacher Wang Xiaohua's GitChat [How to Play Algorithms].1. Exhaustive Law ConceptThe exhaustive search method, also known as the exhaustive search method, is a general term for exhaustively searching all possible solutions in the solution space of the problem doma ...

  2. c#-implement interface in f#

    This is my c# interface:public interface ITemplateService{ string RenderTemplate(object model, string templateName); string RenderTemplate(object model, string templateName, string directory);}I am trying to implement in F#, but I get an error at the end of the keyword. (Unexpected end in implementa ...

  3. F# hurts my brain... Can anyone help translate C# to F#?

    I found a quick project that I think is very suitable for learning F#. However, for some reason, I cannot wrap my brain. After a few hours of tutorials, and even some movies, I still just...don’t understand.So I want to use Enterprise Manager"Generate Scripts"to start version control of our stored p ...

  4. Reserve field names at F#/C# boundary

    As a learning exercise, I'm doing some data analysis on a 5000 row csv file using F# and F#'s type provider functionality. When I use a type provider-generated type in f#, the header row of the csv file naturally becomes that type field name of .type restaurantCsv = CsvProvider<"C:\Users\Paul\Des ...

  5. [python] c, m, F, f, v, p in PyCharm meaning

    The difference between function (Function) and method (Method) in Python:Functions that are not bound to classes and instances belong to functions;Functions that are bound to classes and instances belong to methods. ...

  6. Java programming ideas

    The most important thing in the whole development process is: don't lose yourself!You should always remind yourself of the following issues:(1) What is the object? (How do I split my project into a series of individual components?)(2) What are their interfaces? (What message needs to be sent to each ...

  7. c#-Create native functions in F#

    For example, I have 2 additional functions:module Addlet add2 a = let innerFn a = a + 2 innerFn alet add2' a = let innerFn' () = a + 2 innerFn' ()let res1 = add2 0let res2 = add2' 1As far as I know, both innerFns will be compiled into FSharpFunc<int,int> and FSharpFunc<unit,int> are init ...

  8. F# properties and C# properties

    When developing F# applications, my type contains Lazy<'T> type attributes.Obviously, an interesting side effect (forgive the pun) of the way F# handles the syntactic sugar of properties (as opposed to the C# way) is that property getters and setters may return/accept different types. (At leas ...

  9. F#CSV parsing to C# application

    I have a C# console application that calls the F# library, which does some CSV parsing by using CSVTypeProvider. (http://fsharp.github.io/FSharp.Data/reference/fsharp-data-csvprovider.html )Unfortunately, I am new to F#, so I haven't found a way to efficiently pass parsed data from F# to C# in the form of a list of C# objects.Suppose I have a C# data model:public class Customer{ public int ID { get; set; } public string Name { get; set; } }Then I want to convert the data from the csv type provid ...

  10. 2019 Mini Program Operation Ideas

    In the general trend, the mini program will play its leading role in the e-commerce business in the new year. The mini program value1. The mini program belongs only to you, and the mini program does not need to be maintained like an official account.2. The number of WeChat users is as high as 1 bill ...

  11. python-NameError Use "finally: f.close()" and "with open(..)as f" together

    I have code like below in python. I am using the'with' keyword to open the file and parse its content. But when I try to close the file an error occurs. Please help.Error message:"NameError: name'f' is not defined"try: user_xml_name = raw_input('Enter the xml name: ') xml_name = user_xml_name.replac ...

  12. , what does -f stand for in "php -f"?

    What does"-f"say* * * * * php -f myscript.phprepresent?Solution:According to the PHP command line option reference: http://php.net/manual/en/features.commandline.options.php, the -f option indicates which file the PHP engine should execute, in this case myscript.php. ...

  13. javascript-How to implement f(g)== g(f)

    I answered a question yesterday, and it reminded me of an interesting (for me) puzzleDue to restrictions on the use of lambda, number and only (no if,?: or other language features), the goal is to achieve some f and some g like this// contractf(x) => f'g(y) => g'f'(g') == g'(f')// or more simp ...

  14. nodejs Ideas for uploading front-end and back-end pictures

    ### Picture upload 1. Front-end perspective a. Send the picture to the back-end ajax1. The front-end gets the picture information File field 2. Save the file information to formdata3. Call the back-end The written api interface sends data b. accepts the returned data The front-end page displays the ...

  15. short link generation ideas c#

    After studying the relevant materials, turn around and come back to discover the important position of basic knowledge!One sentence to explain this matter, the practical application of the conversion algorithm in the textbook!In the current Internet API developed cash, how many people can know what ...

  16. F# Concise Tutorial One: Overview of F# and Functional Programming

    F# is a programming language of Microsoft's .NET development platform. Its biggest feature is the introduction of functional programming (FP, Functional Programming); F# also has excellent support for object-oriented (OOP) programming. Use F# language to develop People can freely choose functional p ...

Recent Posts

  1. c#-How to use .Net IRFC function to pass multiple tables to SAP

    I passed the 3 tables to IRfcFunction, which created the order on SAP. When I tried to debug this program, it didn't give any error message, but it was not creating any order on SAP.I am looking for any help or tips to solve this problem. Thank you for your time.IRfcFunction orderDetails = _rfcDest....

  2. c#-Mono.Cecil automatically implements the attribute access backing field

    I am using Mono.Cecil to inject some IL code in the automatically implemented property setter. The problem is that I can reference it from the TypeDefinition.Fields object, but when I use that reference to inject the ldfld instruction (after the ldarg.0 instruction) , It will cause the application t...

  3. c#-How to intercept Http traffic

    I'm trying to develop a C# application that will intercept outgoing Http requests and return the results to the old system. We have a legacy system that makes Http requests to a discontinued web service. I need to intercept incoming requests and Send it to a new web service. When the response comes ...

  4. fast exponentiation modulus algorithm

    Problem introductionFast exponentiation is used to solve the result of \(a ^ n\ mod\ m\).The naive approach is to directly use the loop to solve the problem, the time complexity is \(O(n)\).typedef long long ll; ll power(ll a, ll n, ll m) { ll result = 1; for (int i = 0; i <n; ++i) {result = (res...

  5. java-Use SSH "exec" channel to execute sudo

    I am using the following command to pass the file: >host name > pwd > pbrun su – fclaim > whoami > cd .. > pwd Use the following Java code: for (String command1: commands) { Channel channel=session.openChannel("exec"); ((ChannelExec)channel).setCommand(com...

  6. PHP-How to intercept PDO calls?

    I am trying to implement a plugin API in a PHP-based product I am developing. I created a class that inherits from PHP's PDO class, and then added some additional methods. The trouble is, I want to intercept PDO in the plugin API Things like .query(), .exec(), .execute() and .fetchAll(), which handl...

  7. python-how to handle two versions of the same website

    My project contains two versions of the same thing. For example, version 1 is Freshman and version 2 is a sophomore. To avoid redundancy, I use the same templates and views because the implementations in the two versions are not much different. The only difference in this website is the URL. loca...

  8. c#-.NET network library

    I have been looking for a network library suitable for C#. It will be used with XNA 3.1 and .NET Framework 3.5. The multiplayer style will be server and client. Currently I have been researching Lidgren Library Network but it seems to be outdated.Anyone has a good network library suggestion. It shou...

  9. python iteration

    IterationThe principle of for loop traversalThe principle of for loop traversal is iteration, after in must be an iterable object1. Iterable objectsObjects with __iter__ method are all iterable objects, and there are the following 6 typesIterable objects: Python built-in str, list, tuple, dict, set,...

  10. python function| anonymous function

    Anonymous function: lambda expression. Ordinary functions and only functions that return values can be simplified into one-line functions with anonymous functions. Anonymous functions are not used alone , but are generally used in combination with built-in functions. Among the built-in functio...