I have a file in the website and i m trying to send this file to windows application using the Response.BinaryWrite (getContent)( Where getContent is the byte array having the file which I need to send) vis HTTP post method only. Also I m adding a Header and Content-Type as application/octet-stream in the Response.
Now while reading the (httpWebResponse) response in the stream at client side(windows application) all the things (header + content-type + file + some extra bytes) are getting added. so when I try to read the file in stream it cannot be loaded since the content has chnged
Is there any way to separate the file from rest contents present in the response object..
How sahll I save this file in directory
Use System.Net.WebClient.DownloadData or DownloadFile method instead.
What language / version are you using?
If you are using a reasonably up-to-date version of C# you can use the WebClient class, and its DownloadFile method
Related
Saved ".docx" file with mimeType "application/vnd.openxmlformats-officedocument.wordprocessingml.document". But when I access it's endpoint in Spring Content and download it's not exactly Word document, but Zip archive(application/zip). Spring Content 1.2.5 supports ".docx" files, how can we fix it?
Demo project to reproduce issue(.docx file attached):
https://github.com/leonaugust/docx-problem
EDIT
Although I understand that resulting file is docx after all and we can choose to open it as Word document, but is there a way to make it less confusing for customers and return back as ".docx" format? In my case a huge amount of documents will most likely be sent in that format
As you have spring-content-rest on the classpath and use the #StoreRestResource annotation I am assuming that you are using that to fetch your content? Please let me know if that is not the case and I will edit.
There are a couple of annotations that, if present on the entity, the spring content rest post/put endpoints would set for you and that are then used later on by the GET endpoint; #MimeType and #OriginalFileName.
If you add these annotations to your entity and set them appropriately in your post endpoint then:-
#PostMapping
public UUID create(#RequestParam("file") MultipartFile multipartFile) {
File file = new File();
file.setMimeType(multipartFile.getContentType());
store.setContent(file, multipartFile.getResource());
file.setMimeType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
file.setOriginalFileName(multipartFile.getOriginalFilename());
UUID id = repository.save(file).getId();
log.info("id {}", id);
return id;
}
When your client fetches content (again, I assume) via the Spring Content REST endpoint it will set the following headers:
Content-type header
content-dispostion form-data attachment header
on the response.
Both of which will direct the browser app as to handle the content appropriately.
This should allow you to make the following get request from your browser.
curl -H 'Accept: application/vnd.openxmlformats-officedocument.wordprocessingml.document' http://localhost:8080/storage/b9ca6fbe-dede-4a51-b444-9e22b798e922
And it should download the attachments as test.docx
Seperately, I'd be curious to know why you added your own "create" endpoint, rather than using the Spring Data REST/Spring Content REST endpoint. It will do this for you automatically. I assume it is because you do not want to use Spring Data REST?
I am using Parse Mobile Backend for my android app,but whenever I want to upload a large video more than the default 10mb limit for a ParseFileby getting the bytes from it into a ParseFile I keep running into the dreaded OutOfMemory Exception .So,I want to use the Parse REST API since I can easily use the setChunkedStreamingMode(1024) in HttpUrlConnectionto send the bytes in a chunked manner.The trouble is,how do I get the uploaded url of the file uploaded.Thanks for your help in advance.
i think you can use getUrl() method
ParseFile file = new ParseFile(byte[] data);
file.getUrl();
ParseFile object has getUrl() method to getting url of file. this method return URL in string format.
Now,I have a need that post a file stream,not a local file.the process is:
client(file) ---> my server ----> third party Cloud Storage,the transfer is file stream.
I have found this article:
Ruby: How to post a file via HTTP as multipart/form-data?
require 'rest_client'
RestClient.post('http://localhost:3000/foo',
:name_of_file_param => File.new('/path/to/file'))
you can see that the name_of_file_param is a local file,not stream.
so I want to know ,if this is file stream form the client ,what should I do
You should be able to use any IO object, including a stream, as the parameter:
RestClient.post('http://localhost:3000/foo', :name_of_file_param => my_stream)
I'm using transloadit and sending files with xhr using valumns file uploader.
Files are sent to the server without errors (i checked size and content of request.body.read) but once I do:
response = assembly.submit! (request.body)
An empty file is sent (I checked many times in the assemblies history page).
Thanks for you help.
Call request.body.rewind before using request.body.
location.href,
using hidden Iframe and setting its source dynamically,
setting return false; for onclick
Nothing is working for IE.
Basically, my dwr response generates a log file (foo.log) #business layer and it sends file name as response to dwr rpc request. Now I know the file name and its location I just want to download that file.(It works in FF not in IE).
To cause a web browser to download a file that it would otherwise display inline, you must have the web server serve it with the header:
Content-Disposition: attachment
Nothing you can do on the client-side with iframes or JavaScript will affect this.
worked for me --
Need to write struts action -
inputStream
application/octet-stream
attachment;filename=${fileName}
2048
and action class
download examples
http://jtute.com/codeDownload.html
Struts 2 Design and Programming: A Tutorial chapter 13 a, 13b will give you idea.
No other way we cant set header types in js. we have to action class which will set above mentioned properties.