角 - 节点EX preSS - 大图片上传问题 [英] Angular - Node Express - Large image upload issue

查看:123
本文介绍了角 - 节点EX preSS - 大图片上传问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理使用前press.js,节点和棱角文件上传。基本功能对于小尺寸的图像工作。虽然我尝试尺寸更大的照片,我得到404错误。我用角文件上传。这是我的文件上传的编码

I'm working on handling file uploads using express.js ,node, and angular. The basic functionality working for small size images. While i try to upload larger images, i got 404 error . I use angular-file-upload. This is my file upload codings

$upload.upload({
            url: '/products/image/upload',
            data: {
                PRODUCT_ID : productId,
            },
            file: file
        }).progress(function(evt){
            console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total, 10));
        }).success(function(data){
            console.log(data);

        });

这是我的前preSS JS code

This is my express js code

app.post('/products/image/upload', controller.uploadProductImage);


exports.uploadProductImage = function(req, res){
var fileObject = req.files.file;
var newPath = '';

    var newFilename = 'sample.png';
newPath = './uploads/products/' + newFilename;

fs.rename(fileObject.path, newPath, function(err) {
    if (err) throw err;
    fs.unlink(fileObject.path , function() {
        if (err) throw err;
        });
    });
res.json({success:true});

};

我已经张贴在这里只有样品code。它的工作原理有利于更小尺寸的图像。但是,如果我上传一个大尺寸的图像,路线显示为404的错误消息。我希望这将是一些配置问题,如内存,进程限制。我不知道什么是确切的问题。

I have posted here only the sample code. It works good for smaller size image. But if i upload a large size images, the route is shown as 404 error message. I hope it would be some config issue, like memory, process limit. I am not sure what is the exact problem .

我已经尝试设置前preSS脚本的限制,比如

I have tried to set the limit in express script like

app.use(express.limit('2mb'));

这不是为我工作。我试图

It is not worked for me. And i tried

node --max-old-space-size=2000 app.js

厄运。请帮我解决这个问题。

Bad luck. Please help me to resolve this problem

在此先感谢

推荐答案

我一直在试图让我的头周围采用了棱角分明,节点,和Ex preSS自己的文件上传。我使用防爆preSS V4.1是连接 - 打杂并具有以下节点,防爆preSS code与丹尼尔Farids角文件上传模块的工作。

I have been trying to get my head around file uploads using Angular, Node, and Express myself. I am using Express v4.1 with connect-busboy and have the following Node, Express Code working with Daniel Farids angular-file-upload module.

SERVER.JS

    var express = require('express');       //Express Web Server
    var busboy = require('connect-busboy'); //middleware for form/file upload
    var path = require('path');             //used for file path
    var fs = require('fs-extra');           //File System - for file manipulation
    var util = require('util');         

    var app = express();
    app.use(busboy());
    app.use(express.static(path.join(__dirname, 'public')));

    /* ==========================================================
    Create a Route (/upload) to handle the Form submission
    (handle POST & PUT requests to /upload)
    Express v4 Route definition
    ============================================================ */
    app.route('/upload')
    .post(function (req, res, next) {
      var arr;
      var fstream;
      var filesize = 0;
      req.pipe(req.busboy);

      req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {        
        //uploaded file name, encoding, MIME type
        console.log('File [' + fieldname +']: filename:' + filename + ', encoding:' + 
             encoding + ', MIME type:'+ mimetype);

        //uploaded file size
        file.on('data', function(data) {
          console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
          fileSize = data.length;
          console.log("fileSize= " + fileSize);
        });

        file.on('end', function() {
          console.log('File [' + fieldname + '] ENDed');
          console.log("-------------------------");
        });

        /*
        populate array
        I am collecting file info in data read about the file. It may be more correct to  
        read file data after the file has been saved to img folder i.e. after  
        file.pipe(stream) completes the file size can be got using stats.size as shown 
        below
        */
        arr= [{fieldname: fieldname, filename: filename, encoding: encoding, MIMEtype:   
            mimetype}];

        //Path where image will be uploaded
        fstream = fs.createWriteStream(__dirname + '/img/' + filename);
        //create a writable stream

        file.pipe(fstream);                         //pipe the post data to the file

        //stream Ended - (data written) send the post response
        req.on('end', function () {
        res.writeHead(200, {"content-type":"text/h tml"});  //http response header

        //res.end(JSON.stringify(arr)); //http response body - send json data
      });

      //Finished writing to stream
      fstream.on('finish', function () {
        console.log('Finished writing!');

        //Get file stats (including size) for file saved to server
        fs.stat(__dirname + '/img/' + filename, function(err, stats) {
          if(err)
            throw err;    
        //if a file
          if (stats.isFile()) {
            //console.log("It\'s a file & stats.size= " + JSON.stringify(stats));
            console.log("File size saved to server: " + stats.size);    
            console.log("-----------------------");
          };
        });
      });

       // error
       fstream.on('error', function (err) {
         console.log(err);
       });
      });   // @END/ .req.busboy
    })  // @END/ POST

请参阅[我的GitHub( https://github.com/mick26/ng_Node-AdvancedFileUpload

See [My GitHub] (https://github.com/mick26/ng_Node-AdvancedFileUpload)

与前preSS注4.1很多内置省略前preSS 3.X的中间件。您将无法使用req.files因为该功能被称为可怕的第三方中间件这是使用所谓的bodyParser另一中间件的一部分。这两个强大的和身体的解析器仍然可以在NPM我想。
相反,我用名为连接 - 打杂的NPM模块和流数据。我只是检查程序,并上传> 320M的文件成功。希望这可以帮助你。

Note with express 4.1 alot of the middleware built into express 3.x is omitted. You will not be able to use req.files as this function is part of a third party middleware called 'formidable' which was used by another middleware called 'bodyParser'. Both formidable and body-parser are still available on NPM I think. Instead I used an npm module called connect-busboy and streamed the data. I just checked the program and uploaded a file of >320M successfully. Hope this can help you.

这篇关于角 - 节点EX preSS - 大图片上传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆