在不使用Express的情况下将图像上传到Node.js中的服务器

用户3304517

下面是我的服务器代码,我尝试在其中获取上载的文件。但是,fs.writeFile这不起作用,因此我假设我做错了什么。

  server.on('request', function(request, response){
        ....
         if((pathArray[1] == "photos") && (pathArray[2] = "new")){
            var imagesPath = './images';
            uploadPhoto(response, imagesPath);
          }


       else if(path == '/document/save'){
            console.log("path: " + path);

            var body = '';

            request.on('data', function(data){
                body += data;
            });

            request.on('end', function() {
                var note = querystring.parse(body);
                console.log("Body data: " + note);
                var newPath = "./images/myimage.jpg";
                fs.writeFile( newPath, body, function (err) {
                     if (err) throw err;
                 });
            });




        }   

如果可以帮助任何人,这是我的表单HTML:

 function uploadPhoto(response, imageLoc){  

    response.writeHead(200, {
        'Content-Type': 'text/html' 
    });

    response.write('<html><body>');     
    response.write('<div class="uploadFile">');
    response.write('<link rel="stylesheet" type="text/css" href="style.css">');
    response.write('<form action =/document/save>');
    response.write('<method = "post">');
    response.write('<enctype="multipart/form-data">');
    response.write('<label for="name">Upload new photo</label>');
    response.write('<br></br>');
    response.write('<input type="file" name="name">');
    response.write('<br></br>');
    response.write('<button type="submit">Upload</button>');
    response.write('</div>');

    response.write('</body></html>');
    response.write('</form>');


    response.end();
}

上传文件后,URL转到/document/save/uploadImage.jpg。但是,当我尝试读取图像的内容(“主体”)以将图像保存到文件夹中然后显示它时,请求的对象的内容似乎为空。

我如何使用node.js没有express的图像或除我拥有的以外的任何其他外部库来获取图像的内容fs.writeFile写一个二进制文件时要使用一个很好的功能?

赛亚人女孩

必须考虑的是,从上载接收的数据具有以下格式:

------WebKitFormBoundary9BhXe3lt2UddCDz9
Content-Disposition: form-data; name="document"; filename="globeSS.jpg"
Content-Type: image/jpeg

ÿØÿà JFIF   d d  ÿì Ducky     d  ÿá
//[binary binary binary...]
Ï[leñnœ“}ÛOyŠVÑ0êãXÂ}Ö'±”É iÉöÚ$GTQ7äŽø
uÚ_êÍòXgV¿Õ=€q`]a­KRÐÀ
ò<ÿÙ
------WebKitFormBoundary9BhXe3lt2UddCDz9--

为了只获取二进制数据(进而是文件),程序员必须找出一种从数据中裁剪二进制数据的方法。在下面的代码中,图片的二进制文件全部保存在内存中,因此,如果用户上传特别大的文件,则以下实现可能会失败。最好尝试将文件记录在卡盘中。

request.setEncoding('binary'); 

//Grabbing all data from the image
var body = ''
var binaryEnd; //gets the string that indicates the location of the end of the binary file
var first = true;
request.on('data', function(data) {
    if(first)
        binaryEnd = data.toString().substring(0, data.toString().indexOf('\n')-1);
    first = false;
    body += data
});

//Dealing with the image once we have everything 
request.on('end', function() {      

    var note = querystring.parse(body, '\r\n', ':')
    console.log(note)

    //making sure than an image was submitted 
    if (note['Content-Type'].indexOf("image") != -1)
    {   
        //get the filename 
        var fileInfo = note['Content-Disposition'].split('; ');
        for (value in fileInfo){
            if (fileInfo[value].indexOf("filename=") != -1){
                fileName = fileInfo[value].substring(10, fileInfo[value].length-1); 

                if (fileName.indexOf('\\') != -1)
                    fileName = fileName.substring(fileName.lastIndexOf('\\')+1);
                console.log("My filename: " + fileName); 
            }   
        }

                    //Get the type of the image (eg. image/gif or image/png)
        var entireData = body.toString();           
        var contentTypeRegex = /Content-Type: image\/.*/;

        contentType = note['Content-Type'].substring(1); 

                    //Get the location of the start of the binary file, 
                    //which happens to be where contentType ends
        var upperBoundary = entireData.indexOf(contentType) + contentType.length; 
        var shorterData = entireData.substring(upperBoundary); 

                    //replace trailing and starting spaces 
        var binaryDataAlmost = shorterData.replace(/^\s\s*/, '').replace(/\s\s*$/, '');

                    //Cut the extra things at the end of the data (Webkit stuff)
        var binaryData = binaryDataAlmost.substring(0, binaryDataAlmost.indexOf(firstLine));        

                    //Write to a file 
        fs.writeFile('./images/' + fileName  , binaryData, 'binary', function(err)
        {
                            //forward to another location after writing data 
            response.writeHead(302, {
                'location':'/index.html'
            });
            response.end();
        });
    }
    else
        respond(404, "Please input an image", response); 
});     

这应该在所有浏览器中都可以使用(请注意,Internet Explorer不会使用来限制其数据------WebkitFormBoundary,而是使用其他方式来限制(我认为只是-----,但我忘记了。)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在不使用Express的情况下将图像上传到Node.js中的服务器

来自分类Dev

在我的代码中不使用FTP信息的情况下将图像上传到服务器?

来自分类Dev

如何在不使用C#浏览的情况下将文件从系统中的已知路径上传到Web服务器

来自分类Dev

使用Express和multer将具有Java MultipartEntity的图像上传到Node.js服务器

来自分类Dev

使用多方将图像从iPad应用程序上传到Node JS服务器

来自分类Dev

使用AFNetworking通过node.js将图像上传到服务器失败

来自分类Dev

不使用express.js将图像保存到Node.js服务器

来自分类Dev

没有表单,如何将图像上传到Node JS服务器?

来自分类Dev

使用js将文件上传到服务器

来自分类Dev

将图像上传到服务器

来自分类Dev

使用AsyncTask将图像或视频上传到服务器

来自分类Dev

使用tinymce将图像上传到服务器

来自分类Dev

使用改造将图像上传到服务器

来自分类Dev

使用 cURL 将图像上传到 PHP 服务器?

来自分类Dev

如何在Java中不使用XMLHttpRequest的情况下将数据发送到服务器?

来自分类Dev

如何使用zk框架将图像上传到服务器中的指定文件夹

来自分类Dev

如何在multer中指定目标以将图像上传到节点js中的远程服务器文件夹?

来自分类Dev

使用Node.js,Express,Mongoose和React将图像上传到MongoDB

来自分类Dev

node.js中的Web服务器路径错误

来自分类Dev

无法连接到Android中的Node JS服务器

来自分类Dev

Node.js服务器中没有缓存

来自分类Dev

在Node.js服务器上的内存存储中

来自分类Dev

无法连接到Android中的Node JS服务器

来自分类Dev

制作服务器模块以在node.js中重用

来自分类Dev

在Ubuntu中运行Node JS服务器

来自分类Dev

在Node.js中运行服务器特定的任务

来自分类Dev

node.js服务器中的延迟

来自分类Dev

服务器在node.js中返回的打印变量

来自分类Dev

node.js 中的假 REST 服务器

Related 相关文章

  1. 1

    在不使用Express的情况下将图像上传到Node.js中的服务器

  2. 2

    在我的代码中不使用FTP信息的情况下将图像上传到服务器?

  3. 3

    如何在不使用C#浏览的情况下将文件从系统中的已知路径上传到Web服务器

  4. 4

    使用Express和multer将具有Java MultipartEntity的图像上传到Node.js服务器

  5. 5

    使用多方将图像从iPad应用程序上传到Node JS服务器

  6. 6

    使用AFNetworking通过node.js将图像上传到服务器失败

  7. 7

    不使用express.js将图像保存到Node.js服务器

  8. 8

    没有表单,如何将图像上传到Node JS服务器?

  9. 9

    使用js将文件上传到服务器

  10. 10

    将图像上传到服务器

  11. 11

    使用AsyncTask将图像或视频上传到服务器

  12. 12

    使用tinymce将图像上传到服务器

  13. 13

    使用改造将图像上传到服务器

  14. 14

    使用 cURL 将图像上传到 PHP 服务器?

  15. 15

    如何在Java中不使用XMLHttpRequest的情况下将数据发送到服务器?

  16. 16

    如何使用zk框架将图像上传到服务器中的指定文件夹

  17. 17

    如何在multer中指定目标以将图像上传到节点js中的远程服务器文件夹?

  18. 18

    使用Node.js,Express,Mongoose和React将图像上传到MongoDB

  19. 19

    node.js中的Web服务器路径错误

  20. 20

    无法连接到Android中的Node JS服务器

  21. 21

    Node.js服务器中没有缓存

  22. 22

    在Node.js服务器上的内存存储中

  23. 23

    无法连接到Android中的Node JS服务器

  24. 24

    制作服务器模块以在node.js中重用

  25. 25

    在Ubuntu中运行Node JS服务器

  26. 26

    在Node.js中运行服务器特定的任务

  27. 27

    node.js服务器中的延迟

  28. 28

    服务器在node.js中返回的打印变量

  29. 29

    node.js 中的假 REST 服务器

热门标签

归档