Home  >  Q&A  >  body text

node.js - 如何用node实现上传头像时的预览和裁剪呢?

如题,我想实现一个上传头像的效果,大多数网站那样的效果,选择完一张图片后可以先预览,并且适当调整,满意了再进行上传修改,但是貌似由于安全问题,js是不允许直接打开本地文件的,所以要想实现预览的效果这就要先把图片上传到服务端,再从服务端传回来。但是个过程要用什么方法实现呢?我用表单把图片传到服务端后就不知道该怎么再传回去了。。。

大家讲道理大家讲道理2765 days ago557

reply all(2)I'll reply

  • 阿神

    阿神2017-04-17 11:36:06

    To correct the wrong impression about js, js can read local files, but it only needs to be triggered by user operations. For example, when uploading a picture, the user needs to click the "select picture" button.
    The specific operation uses HTML5’s File API
    detailed explanation and demo

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 11:36:06

    After the upload is completed, there is always a URL that can be accessed from the outside. This URL is returned to the browser, and then the browser loads the image, and uses the JS library that can cut the image. After the user operates the cut, X, Y, W, H continue to be transmitted to the background and you can use the picture library in the background for cropping.

    Related recommendations:
    http://deepliquid.com/content/Jcrop.html
    https://www.npmjs.com/package/gm

    reply
    0
  • Cancelreply