Download multiple files using httpservlet - javascript

I am trying to implement a function where on a page like this you first select the records to export/download, after clicking download button and fetching records from db, the information will be saved in two ways:
for text info, they will be written in a xls file together and then downloaded
for pics, they will be packed in a zip file first and downloaded.
Front-end request is formed in this way
var form = $("form");
form.css(...)
.attr("target","_blank")
.attr("method","post")
.attr("action","export")
....
.submit()
For back-end, I used HttpServlet with some routine operations like
response.setContentType();
response.setHeader("Content-Disposition","attachment;filename"+fileName+".zip");
out = response.getOutputStream();
out.write(buffer,0,b);
in zip download, also ZipOutputStream was used like
zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()))
...
zipos.putNextEntry(new ZipEntry(...))
When I test the two downloads separately, it worked fine for excel but for Zip file I was shown error like this.
When I tried to test with two form.submit() requests, only the first one will be responsed.
Here are my questions:
What's reason for error in zip file? I tried to setContentType to "multipart/form-data","zip","gzip","application/octet-stream". Neither worked.
Does my way for two sole requests make sense? What's the best way to realize the two/multiple file download functionality?
Thanks.

Related

Automatic file downloader

I want to download files from a web page that offers public downloads.
I have little experience with the world of web pages, so initially I wrote a simple script using selenium that solves the problem, however I find this way of getting the data too twisted. Therefore, I have tried to download the file via POST requests, investigating a little the "Network" panel of tools I managed to find the request that apparently orders the download.
The button that appears surrounded in the image launches the three requests that appear on the right, except the first time the file is downloaded, one more request appears (this can be seen above the highlighted requests).
However, the response of this request, far from being the content that would allow me to write the desired file, is more like a fragment of html code that corresponds to the content displayed when in the network panel I double click on "downloadDir":
What could I be missing? Does anyone know how to solve the problem or it is not possible by this way?
You should use requests package instead of executing curl or other processes:
import requests
response = requests.post('https://localhost:4000/bananas/12345.png', data = '[ 1, 2, 3, 4 ]')
data = response.content
data contains the downloaded content after that, which you can store to disk for instance:
with open(path, 'wb') as s:
s.write(data)

Ionic - File upload without reading into memory

I'm trying to find a way to upload large files without having to read them all into memory. I need to be able to post multiple files along with a data payload in a single request. I'm able to accomplish it with standard HTML inputs, something like this:
<input id="myFile1" type="file" />
<input id="myFile2" type="file" />
<input id="myData" type="text" />
And javascript code like this:
var file1 = document.getElementById('myFile1').files[0];
var file2 = document.getElementById('myFile2').files[0];
var data = document.getElementById('myData').value;
var formData = new FormData();
formData.append('myFile1', file1, file1.name);
formData.append('myFile2', file2, file2.name);
formData.append('myData', data);
Even when selecting a large file this is very fast. Keep in mind I'm only referring to the client side performance, as the actual upload portion is going to be bottlenecked by the users network speed.
So now comes my problem: I need to use the Ionic camera plugin to allow a user to either record video or select video files on their phone. But when a user picks a file, you aren't immediately given a File object to work with; a user picks a file, then you are given a file URL and have to convert that into a Blob in order to post it to a server. However, as you can see from this stackoverflow answer, you have to read the entire file contents into memory to create the Blob object. This is slow (and very inefficient) when selecting a large file.
Does anyone know of a way to get a Blob or File object without reading everything into memory? I looked at the File and Blob API documentation but from what I see there isn't a way, unless I'm missing something. I'm wondering how the File API works behind the scenes when using a standard HTML input and choosing a file, because as shown by the example above, you're immediately given access to a File object (which is also a Blob) and it's very fast even for large files.

Remote form - XLSX download

I have a form, where user can select values from collection of check boxes to filter data. Form posts to controller where xlsx format is defined to download xlsx file. I am using axslx_rails.
Now, I would like something to happen before and after the remote call.
And it works ok.
What I can't figure out is how to actually download a xlsx file as the respond block goes to JS format, not xlsx, which is something liek this:
format.xlsx { response.headers['Content-Disposition'] = 'attachment; filename="test.xlsx"'}
So, it works either one or the other way. Non-remote form downloads file as i have a format: xlsx defined. If i do a remote form, javascript works (show spinner, hide spinner, etc...), but file is not downloaded.
How can I achieve both?
Thx
OK, i've given up and went with a solution:
to save file to disk (to some publicly accessible location)
download file via jQuery fileDownload plugin
delete file
Seems like the best solution currently for what i need.
If someone has a more elegant solution, without extra JS library involved...

Upload/download/save image to a local server from a remote server using an Image URL and PHP?

I am building a Discussion Forum as part of a bigger application I am building, the forum is just 1 section of the Application.
For my TextArea fields when posting a new Topic or a Post Reply, I have decided that nothing is as good as the PageDown Markdown Library. It is the same one that StackOverflow uses on all their sites and it works better than many of it's competitors.
The way the library ships though, I am not happy with the default Insert Image functionality. You hit the button to insert an image and it allows you to enter a URL for an Image and then it inserts the proper MarkDown syntax to show the linked image.
This just won't cut it. I need the functionality that you see on StackOverflow! Very similar anyways.
I need it to show a Dialog when you click the Insert Image button, like it does now, but instead of just an input field for a Image URL, it will have 2 filed options...
Upload image from your computer
Insert an Image URL and it will then DOWNLOAD the image from that URL and insert it into the post just as if you had uploaded it from your computer. This is important to not confuse this step. IT should not simply insert the Image linking it to the original Image URL. Instead it will take that URL and download/upload the Image to the same server that the upload from computer option does and then it will insert the NEW Image URL pointing to the newly uploaded image!
Based on some simple HTML like below for a Dialog window with a filed for my Upload from Computer functionality, which I already have working. I need to come up with some JavaScript and PHP that will download/save a remote image to my upload folder on my server when a button is clicked using only the URL that will be inside the URL text input field.
So it will need to do a few things...
Fetch and save an image file to my uploads folder using PHP when the only thing that the PHP function will receive is a URL of the image which could be on the same server or most likely a remote server.
After successfully saving/uploading an image from the URL, the PHP function will return a JSON string with the status/error and if successful then it will also return the actual URL and filename of where the new image is saved on the local server. The JavaScript/AJAX script will receive this JSON response and insert the Markdown syntax for the image into the PageDown editor.
The PHP function will need to ensure that the URL that it is trying to save/download is a valid image file and not some malicious file! Also not simply just some file of the wrong filetype like a non-image file unless we are allowing the file type.
It will be part of a module installed on many dinosaur servers so it needs to work on as many servers as possible too!
From the web
From your computer
I would be greatful of any help, tips, code snippets or anything to help with this. At this stage I really just need to build a nie PHP function that will upload images from a remote URL and also ensure that the URL passed in is a real image file or even better that it is in the allowed file types array!
A couple years ago I had started this but have now lost it and I am starting over and don't remeber much about how I went about doing it then.
The easiest way to download a file from a remote server would be to use copy (http://php.net/manual/en/function.copy.php):
copy('http://someurl.com/image.png', '/var/www/uploads/image.png');
As this function returns a bool, it is easy to determine whether the operation was successful and create a JSON response.
To verify that the file is an actual image, there is unfortunately no way that is 100% sure. It is probably enough to check the mimetype though. You can use finfo for that (http://php.net/manual/en/function.finfo-file.php):
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, $filename);
finfo_close($finfo);
For a gif, this would return image/gif for example. You will have to hardcode a list of all mimetypes you want to allow.

Need to do bulk file upload in JavaScript

I have a little bit of an unusual situation I guess. I have a page for placing new orders and part of a new order is a variable (0-n) number of files that are to be uploaded and associated with the order on the back end. The user also needs to specify a description for each file.
I've used a couple jQuery upload plug-ins with great success, but in this case I'm not looking to upload a single file when the user hits "OK." What I really need to do is upload a file by passing a local path to some method that will do the upload.
Does anyone know of any plug-ins that do this?
Thanks!
Ajax Uploader could be helpful? I believe it allows multiple uploads.

Categories

Resources