1

This is my server code

app.post('/tde/api/photo/:widgetId/:choosenFileName',function(req,res){
console.log("In file Upload..");
console.log(req.params.widgetId);
console.log(req.params.choosenFileName);
res.writeHead(200, { 'Content-Type': 'application/binary' });
var filedata = '';
req.setEncoding('binary');
req.on('data', function(chunk){
    filedata+= chunk;
})
req.on('end', function (chunk) {

        var dir = 'uploads/'+req.params.widgetId
        if (!fs.existsSync(dir)){
            fs.mkdirSync(dir);
            console.log("directory created..");
        }


        var fileName = req.params.choosenFileName;
        var widgetId = req.params.widgetId;
            fs.writeFile('uploads/'+widgetId+'/'+fileName, filedata, 'binary', function(err) {
            if (err) {
                return console.error(err);
            }
        })  
    });
res.end("File is uploaded");

})

This is my client side code. This is my html file which allows user to choose a file and then upload it to the server location

    <div class="col-xs-8">
Upload Image
                                        <span class="warn_msg" ng-if="warningModalWidgetSettingsURLMessage1 != ''"><i class="glyphicon glyphicon-remove-circle"></i> {{warningModalWidgetSettingsURLMessage1}} </span>
                                    </div>
This is my js file which calls the api of nodejs server

$scope.widgetForm.imageConfig = {};
    var imageLocationUrl = "http://10.100.10.140/opt/TDE/tde_v2.0-19-01-2016Sanmoy/uploads/"
    var  imageWidgetId = "";
    var choosenFileName = "";
    $scope.submitFile = function(widgetId){
        imageWidgetId = widgetId
        console.log($scope.fileImage)//this is the file name which I a
        choosenFileName = $scope.fileImage.name
        $http({
            url: "http://10.100.10.140:9001/tde/api/photo/"+widgetId+"/"+choosenFileName,
            method: "POST",
            headers: {
                'Content-Type': 'application/binary'
            },
            data: $scope.fileImage,
            processData: false,
                contentType: false
        }).success(function(data, status) {
            console.log("File Uploaded..")
        }).error(function(data, status) {
            console.log("File Upload fail..")
        })
    }

file is getting uploaded in the server but sometimes it is getting corrupted,can't understand where the problem is.

sanmoy paul
  • 209
  • 2
  • 13

1 Answers1

2

It seems that you're overwriting 'uploads/report.jpg' every time an upload is done and this may cause corruptions during multiple uploads at the same time:

function(err) {
    var fileName = req.params.choosenFileName;
    var widgetId = req.params.widgetId;
    fs.writeFile('uploads/report.jpg', chunk,  function(err) {
        if (err) {
            return console.error(err);
        }
    })  

What about the commented line:

//fs.writeFile('uploads/'+req.params.widgetId+'/sanmoy.jpg', chunk, 

It might be better this way:

fs.writeFile('uploads/'+req.params.widgetId+'/'+fileName', chunk, 

Please note that it would still be possible to upload two or more files with the same 'fileName', so adding some kind of prefix or suffix might be better.

UPDATE 1:

I just noticed that you're only saving the first chunk. The data may fit into one chunk, but it may also consist of multiple chunks. Try something like that:

var filedata = '';
req.setEncoding('binary');

req.on('data', function(chunk){
    filedata+= chunk;
})

req.on('end', function(){
    // create directory etc.
    fs.writeFile('uploads/'+req.params.widgetId+'/'+fileName, filedata, 'binary', function(err){
        if (err) {
            return console.error(err);
    })
})
CupRacer
  • 149
  • 7