How to get pictures from mobile phone in nodejs

WBOY
Release: 2023-05-25 11:10:37
Original
701 people have browsed it

How Node.js gets pictures from mobile phones

Node.js is a back-end server technology based on JavaScript. It has the advantages of high efficiency, flexibility, easy to learn and use, and is now widely used in Web development. and other fields.

As the mobile device market continues to expand, more and more people are using smartphones to take photos, store and share photos. However, to operate these photos in a web application, you need to obtain the photos in the mobile phone and upload them to the server, which requires using Node.js to obtain the photos in the mobile phone.

This article will introduce how to use Node.js to obtain photos from the mobile phone and implement the function of uploading them to the server.

Step 1: Install the necessary modules

Getting photos on your phone requires the use of two Node.js modules, node-exiftool and formidable, so you need to install them first.

You can use npm to install node-exiftool. npm is the package management tool for Node.js. It can easily download and install Node.js modules. Enter the following command:

npm install node-exiftool

Use npm to install formidable, enter the following command:

npm install formidable

Step 2: Write code

To obtain photos on the phone, you need to use the http module of Node.js to listen to client requests, and use the node-exiftool module to obtain the metadata information of the photos. Then use the formidable module to upload the image to the server.

The following is the code to get the photo:

var http = require('http');
var exiftool = require('node-exiftool');
var formidable = require('formidable');

http.createServer(function (req, res) {
  if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      if (err) throw err;
      var ep = new exiftool.ExiftoolProcess();
      ep.open();
      ep.readMetadata(files.file.path, ['-File:all'], function (error, metadata) {
        if (error) throw error;
        console.log(metadata);
      });
      res.writeHead(200, {'Content-Type': 'text/html'});
      res.end('success');
    });
    return;
  } 
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(`
    <form action="/upload" method="post" enctype="multipart/form-data">
      <div>
        <label>Select image:</label>
        <input type="file" name="file">
      </div>
      <div>
        <button type="submit">Upload</button>
      </div>
    </form>
  `);
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');
Copy after login

With the above code, you can start the application with the node command and enter http://127.0.0.1:8080/ in the browser. test.

Step 3: Obtain metadata information

Using the node-exiftool module to obtain metadata information requires opening an exiftool process and using the readMetadata method to read the metadata information of the image.

First, you need to introduce node-exiftool into the code:

var exiftool = require('node-exiftool');
Copy after login

Then, you need to instantiate an exiftool process and use the open method to open the process:

var ep = new exiftool.ExiftoolProcess();
ep.open();
Copy after login

Finally, use The readMetadata method reads the metadata information of the image:

ep.readMetadata(files.file.path, ['-File:all'], function (error, metadata) {
  if (error) throw error;
  console.log(metadata);
});
Copy after login

The readMetadata method needs to receive three parameters: the path of the file, parameter options and callback function. In the above code, the '-File:all' parameter option is used to obtain all metadata information. The metadata obtained by the callback function is a metadata object.

Step 4: Upload photos to the server

Using the formidable module to upload pictures to the server requires the help of the formidable IncomingForm class. The parse method of this class can parse the form data and obtain the uploaded document.

First, you need to introduce the formidable module into the code:

var formidable = require('formidable');
Copy after login

Then, parse the form data through the parse method of the IncomingForm class and get the uploaded file:

var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
  if (err) throw err;
});
Copy after login

In files In the object, you can obtain relevant information about the uploaded file, such as file name, file size, file type, temporary file path, etc.

Finally, you can use the built-in fs module of Node.js to save the file to the server:

var fs = require('fs');
fs.rename(files.file.path, __dirname + '/uploaded/' + files.file.name, function (err) {
  if (err) throw err;
});
Copy after login

The rename method moves the temporary file to the specified directory on the server, __dirname indicates where the current file is located Directory.

Step 5: Complete code example

Integrating the above steps, you can get a complete Node.js code example, which realizes the function of obtaining photos from the mobile phone and uploading them to the server.

var http = require('http');
var exiftool = require('node-exiftool');
var formidable = require('formidable');
var fs = require('fs');

http.createServer(function (req, res) {
  if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      if (err) throw err;
      var ep = new exiftool.ExiftoolProcess();
      ep.open();
      ep.readMetadata(files.file.path, ['-File:all'], function (error, metadata) {
        if (error) throw error;
        console.log(metadata);
      });
      fs.rename(files.file.path, __dirname + '/uploaded/' + files.file.name, function (err) {
        if (err) throw err;
      });
      res.writeHead(200, {'Content-Type': 'text/html'});
      res.end('success');
    });
    return;
  } 
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(`
    <form action="/upload" method="post" enctype="multipart/form-data">
      <div>
        <label>Select image:</label>
        <input type="file" name="file">
      </div>
      <div>
        <button type="submit">Upload</button>
      </div>
    </form>
  `);
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');
Copy after login

Conclusion

The above is an introduction to how to use Node.js to obtain photos in the mobile phone. By using the http module, node-exiftool module and formidable module of Node.js, you can easily Implement the function of uploading photos in the mobile phone to the server. In addition, security and stability need to be paid attention to during the development process, and files uploaded by users need to be correctly verified and processed.

The above is the detailed content of How to get pictures from mobile phone in nodejs. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template