danialfarid / ng-file-upload

Lightweight Angular directive to upload files with optional FileAPI shim for cross browser support
MIT License
7.87k stars 1.59k forks source link

null is treated "null" when Upload a file with other json info #1919

Open CarlosCamposGalo opened 7 years ago

CarlosCamposGalo commented 7 years ago

Consider uploading a file with json

{
  data : {
   field1: null
 }
}

this will be sent in a form

------WebKitFormBoundaryRP6pVWtHY6Sp5zSk
Content-Disposition: form-data; name="data[field1]"

null

In the server side data.field1 = "null" string not null

CarlosCamposGalo commented 7 years ago

As of now I added a workaround as below but I am hoping there should be like this inside upload method to avoid sending "null" string to server:

var objectCleanUp = function (obj) {
     var propNames = Object.getOwnPropertyNames(obj);
     for (var i = 0; i < propNames.length; i++) {
         var propName = propNames[i];
          if (obj[propName] === null || obj[propName] === undefined) {
                    delete obj[propName];
           }
     }
}
var form ={
  data : {
   field1: null,
   field2: 'Something'
 }
}
objectCleanUp(form.data)

Upload.upload({
       url: appRoot + 'api/casefiles/' + dialogCtrl.model.id + '/arrestedpersons'
        data: form
})
.then()
.catch()
saadbinakhlaq commented 6 years ago

Can anything be done for this one, because $http.post is sending null as nil to the server but this angular upload sends it as 'null' as a string.

martinib77 commented 6 years ago

I'm having the same situation Any workarounds ?

RipleWare commented 4 years ago

Did this ever get resolved? I have just come across this now and have been pulling my hair out thinking it was a Laravel issue.

eduardojesustun commented 2 years ago

I'm having the same situation

Some answer?

RipleWare commented 2 years ago

@eduardojesustun, I added the workaround mentioned above by CarlosCamposGalo and it worked for me. I just pass the object through the Cleanup routine before sending it as part of the Upload method and all nulls are handled correctly.

I haven't heard of a fix as yet, but the workaround is working for me and has for quite some time now.

eduardojesustun commented 2 years ago

@RipleWare Thank you, I will choose to carry out the same process, I have many forms to apply it to