1

Hi I am developing web application in angularjs. I am developing file upload module. I have below array with file details.

//below code to get array of files

$scope.showPicker=function()
            {
                var client = filestack.init('AGeDIRvVZTRWgtmFbfGuZz');
                client.pick({
                }).then(function (result) {
                    arrMakes.push(result.filesUploaded);
                });
            }

FileDetails(array)

In the above image i shown my array. I have three files. Below is my angular code to send details to api.

var files = new FormData();

angular.forEach(arrMakes, function (value, index) {
  console.log(value,index);
  files.append(index, value);
  files.append('data', angular.toJson(index).replace(/['"]+/g, ''));
});

return $http.post(this.uploadUrl, files, {
  transformRequest: angular.identity,
  headers: {
    'Content-Type': undefined,
  }
})

The problem is i am not receiving file in server side. Below line gives me 0 files in server.

System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;

May i know am i sending correct data to server? Can someone help me to fix this? Any help would be greatly appreciated. Thank you.

3
  • Where is your form data declaration? Commented Aug 4, 2017 at 11:49
  • var files = new FormData(); i forgot to post. Commented Aug 4, 2017 at 11:49
  • May i get some example please! Commented Aug 4, 2017 at 11:54

1 Answer 1

2

You are not uploading any files to the server, only strings.

You can't append objects to a FormData. appart from Blob & File objects
See what will happen:

fd = new FormData
fd.append(2, {foo: 'bar'})
fd.append('data', 5)
new Response(fd).text().then(console.log)
// you get [object Object]

Why do you stringify the index in "data"? It will be casted to string automatically. And what is there that you have to replace?

If i where you i would just simple send the hole arrMakes to the server and download all the files from the url on the backend, otherwise the client has to download and then upload them to the server and wasting bandwidth and time.

beside, you don't need angulars forEach loop, arrays has that method built in

arrMakes.forEach(function (value, index) {
  ...
})

You won't even have to use any loop if you just pass the arrMarks to the server

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your response. I left office. I will check on Monday.
Thank you for your work. Still i am not getting files in server. I have added arrMakes.forEach(function (value, index) { files.append(index, value); files.append('data',index); })
Still, arrMakes dosen't contains any files, only plain objects. And you can't append objects to to formData as demonstrated above.
So How can i send files? Is it possible?
How about just doing: $http.post(this.uploadUrl, arrMakes) and then on the server parse the response as json and then download each file from every url in the array?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.