1

The question is rather simple if you have Any server side scripting techniques, but in the case of handling[viz. sending] the file as-is to the jSON function.

The scenario is :

  1. I have a fileupload control

    <input type="file" id="fileselect" name="fileselect[]" multiple="multiple" />

  2. with the help of javascript or jQuery, I need to take the uploaded file of this control on "upload" button click. I need to POST this file to the jSON.

  3. Why need to post it as-is is because the file can be .zip or .text [Image file wont be used], I have functions in my Controllers that will work on that file[like Extract/Save/Process, etc]

Any good way that works on all browsers to do this ?

Also saving the file to the local disk using javascript is a good solution in this scenario rather than passing it to jSON ? Please comment on that also.

2 Answers2

0

With PHP it can be be done. $_FILES["fileselect"] can hold on to file arrtibutes , on upload its stored in a temp location , which can be changed by move_upload_file($source,$destination)

Bhupendra
  • 1,196
  • 16
  • 39
  • I am using asp.net, so using php will not be logically correct, i guess. But if I have to, how can I achieve this ? – user3555860 Apr 21 '14 at 12:30
0

For sending the request to the server side without server scripts see my answer here. You can upload the file to the server using XMLHttpRequest. The answer is specifically about how to show a progress bar, but it also shows how to upload the files.

For the option of saving files locally using javascript, see here (file system API) and here (localstorage). Also see this answer which describes the FileReader and File API. Note that you will need to use some relatively recent features, you can refer to caniuse to see if your targeted browsers are supported.

Update

As per the comments you asked how to do this with Web-API Controller. See this answer. Basically, you check and loop through each file in HttpRequest.Files in the controller.

You can't use HttpRequest in self-hosted web API, see here, here and here for alternatives.

Community
  • 1
  • 1
acarlon
  • 16,764
  • 7
  • 75
  • 94
  • Thanks for the Reply. I am trying one of your solution of using XMLHttpRequest. I didn't mention above. I am not using **MVC Controller**. I am using **Web-API Controller**. I an unable to access the HttpPostedFileBase userFile there. is there any way to pass the Parameter in this statement ? xhr.open("post", url, true); – user3555860 Apr 21 '14 at 12:27
  • – user3555860 Apr 22 '14 at 09:10
  • ^ somehow i was not able to add linebreak in above comment. sorry for that. I was trying your solution. But it was throwing error of Nullreference in my web-Api while getting **Current** of **HttpContext** in **HttpContext.Current.Request** !! Why ? – user3555860 Apr 22 '14 at 09:17
  • @user3555860 - see the last line that I have just added. – acarlon Apr 22 '14 at 09:41
  • what is the feasible workaround to this problem then ? How can i do this? – user3555860 Apr 22 '14 at 11:16
  • @user3555860 - see here: http://www.lybecker.com/blog/2013/06/26/accessing-http-request-from-asp-net-web-api/ – acarlon Apr 22 '14 at 11:27
  • Finally, I have to forcefully do this by Server side coding. – user3555860 Apr 23 '14 at 06:45