node.js使用socketio-file-upload从ipad上传图片

时间:2014-03-05 19:28:33

标签: node.js express socket.io

我正在express.js / node.js

中编写应用程序

我有一个表单,上传图片的输入类型为“文件”。

我想直接上传我的图片,而无需用户确认。并在裁剪后显示在用户屏幕上并存储到目录中。

为此,我使用socketio-file-upload库。

它在计算机上运行良好。我可以上传我的图像并显示它们。

问题在于Ipad(我还没有尝试过Iphone)

在网页应用中,当我点击“浏览”按钮时,我可以选择现有图片或拍照。我尝试拍照并上传,但没有任何事情......

以下是上传服务器的代码:

io.of('/register').on('connection', function (socket) {
/***************FILE UPLOAD***********************/
// Make an instance of socketioFileUploadServer and listen on this socket:
var uploader = new socketioFileUploadServer();
uploader.dir = __dirname + '/client/tmp/markers';
uploader.listen(socket);
// Do something when a file is saved:
uploader.on('saved', function(event){
    console.log('Original saved');
    // resize and rename image with a unique id
    var newName = Math.random().toString(36).substr(2, 9);
    // marker 32x32
    easyimg.rescrop({src:path.resolve(__dirname, 'client/tmp/markers/' + event.file.name), dst:path.resolve(__dirname, 'client/tmp/markers/' + newName + '_marker'), width:32, height:32}, function(err, image) {
        if (err) throw err;
        console.log('Resized and cropped: ' + image.width + ' x ' + image.height);
        // marker is uploaded - resized - croped, now display it
        socket.emit('displayMarker', {markerPath : '/tmp/markers/' + newName + '_marker', markerName : newName});
        // remove original from file system
        fs.unlink(path.resolve(__dirname, 'client/tmp/markers/' + event.file.name), function(){
            if (err) throw err;
            console.log('Original removed'); 
        });
    });
});

uploader.on('start', function(event){
    console.log('Client start upload');
    socket.emit('displayOverlay');
});

// Error handler:
uploader.on('error', function(event){
    console.log("Error from uploader", event);
});
});

客户方:

document.addEventListener("DOMContentLoaded", function(){

// REGISTRATION MARKER 
var socketMarker    = io.connect('/register'); 
var siofuMarker     = new SocketIOFileUpload(socketMarker);

siofuMarker.listenOnInput(document.getElementById("uploadMarkerFromExploratorInput"));

// Do something when a file is uploaded:
siofuMarker.addEventListener("complete", function(event){
   $('.overlay').hide(); 
});

// display loader window
socketMarker.on('displayOverlay', displayOverlay);

// server send we can display the marker in the register step1 view
socketMarker.on('displayMarker', function(data) {
    $('#markerImage').html('');
    $('#markerImage').html('<img src="' + data.markerPath + '" />');
    $('#markerImageName').val(data.markerName);
});

});

使用计算机,控制台将显示:

Client start upload
Original saved
Resized and cropped: 32 x 32
Original removed

和Ipad一起:

Client start upload

有人有想法或其他方式获得相同的结果吗?

Apreciate your help。下进行。

1 个答案:

答案 0 :(得分:0)

我怀疑,你需要为File Input元素的HTML标记添加适当的属性:

<input type="file" accept="image/*" capture="camera">

这应该可以解决问题。要理解的最重要的部分是“接受”属性。