mscdex / connect-busboy

Connect middleware for busboy
MIT License
155 stars 25 forks source link

Issue upload files on iPad / iPhone #15

Closed canvas-manik closed 2 years ago

canvas-manik commented 8 years ago

Hi,

I am using connect-busboy to upload image files on my server. It works fine with all the desktop and android devices. But it is failing for the iPad/iPhone for bigger files >=1MB. On desktop,it is working with all types of files. On iPhone/iPad,it is working with small images. Is there some limitation using that? I am using angular-file-upload on the client. I tried using the native javascript XML HTTP request as well. There also, it fails for big files.

Regards, Manik Mittal

mscdex commented 8 years ago

You'll have to show relevant client and server code.

Also, what do you mean "it fails?" Do you get an error? Does the connection hang? Something else?

canvas-manik commented 8 years ago

Thank you for the response. Following is the angular client side code:

fileUpload.html:

<h4>Upload on file select</h4>
<button id="fileUpload" type="file" ngf-select="uploadFiles($file, $invalidFiles)"
        accept="image/*">Select File</button>
<br><br>
File:
<div>{{f.name}} {{errFile.name}} {{errFile.$error}} {{errFile.$errorParam}}
      <span class="progress" ng-show="f.progress >= 0">
          <div style="width:{{f.progress}}%"
               ng-bind="f.progress + '%'"></div>
      </span>
</div>
{{errorMsg}}
<div ng-if='f.uploaded'>File Uploaded</div>

controller.js:

$scope.uploadFiles = function(file, errFiles) {
            $scope.f = file;
            $scope.f.uploaded=false;
            $scope.errFile = errFiles && errFiles[0];
            if (file) {
                file.upload = Upload.upload({
                    url: '<api url>',
                    data: {file: file}
                });

                file.upload.then(function (response) {
                    $scope.f.uploaded=true;
                    $timeout(function () {
                        file.result = response.data;
                    });
                }, function (response) {
                    if (response.status > 0)
                        $scope.errorMsg = response.status + ': ' + response.data;
                }, function (evt) {
                    file.progress = Math.min(100, parseInt(100.0 *
                        evt.loaded / evt.total));
                });
            }
        }

Server side code:

function uploadVanilla(req,res) {
    req.busboy.on('file', function(fieldname, filestream, filename, encoding, mimetype) {
        console.log('inside busboy');
        var uid = shortid.generate();
        var fileExtension = filename.split(".").pop();
        //elapsed_time("Before Start file: " +  filename);
        filename = uid + '.' + fileExtension;
        var srcImgLocation = path.join(process.cwd(), 'uploads/sourceimages', path.basename(filename));
        var writestream = fs.createWriteStream(srcImgLocation);
        var r = filestream.pipe(writestream);
        r.on('finish',function(){
            console.log('File stream finished sucessfully');
            filestream.unpipe(writestream);
            elapsed_time("File Saved on server: " +  filename);
            var result={};
            result.filename = filename;
            res.status(200).send(result);
        }).on('error',function(err){
            console.log('file stream errored',err);
            filestream.unpipe(writestream);
            writestream.end();
            r=null;
            writestream=null;
            filestream=null;
            return utils.handleError(err, res, logger, messages.UPLOAD.ERROR_IN_STREAM);
        });

    });
    req.pipe(req.busboy);
}

If I upload a small image, then it works fine. If the image is bigger, it gets stuck and does not reach the the point 'File stream finished sucessfully' in api. By fails I mean, It does not give any error. It gets stuck. Hope it explains. Regards, Manik Mittal

EDIT: added code formatting

mscdex commented 8 years ago

If you add filestream.on('end', function() { console.log('file stream ended'); }); inside the file event handler, do you see that logged to the console?

canvas-manik commented 8 years ago

Didn't reach there as well.

mscdex commented 2 years ago

If you can reproduce this with the latest version of busboy then please post an issue to the busboy issue tracker.