Is it possible to upload file via request factory? Simple example will be really helpful.
Actually you can!, I have an application doing it already.
You need browsers supporting the FileApi (modern browsers do)
You have to write some jsni code to read the file content into a base64 string.
You will receive (asynchronously) a string which you can assign to any Bean attribute in your app and send it via RF, RPC, etc.
Here you have a copy/paste of the most significant code i use:
public final native void readAsDataURL(MyClass that, FileUpload input) /*-{
var files = input.#com.google.gwt.user.client.ui.FileUpload::getElement()().files;
var reader = new FileReader();
reader.onload = function (evt) {
that.#...MyClass::done(Ljava/lang/String;)(evt.target.result);
}
reader.readAsDataURL(files[0]);
}-*/;
It would be a comming-soon feature on my gwtupload library.
No.
You need to create a separate file upload servlet. See Basic File upload in GWT.
IMO RPC or Request Factory sense is XMLHttpRequest, which does not allow you encode and send local files to a server.
You need to write your own servlet and a GWT FormPanel .
complete example here with servlet and its mapping
Related
I would like to know if there is a way to send an email with attachment file with GWT.
I managed to send a simple email without attachment, but I am having a problem when I try to add a file.
The problem is that "FileUpload" don't give the fullpath of the file
it seems for safety reasons it is impossible to retrieve the full path of the file from the client.
Is there another way keeping the logical server in gwt client?
My code
Client side:
FileUpload upload = new FileUpload();
// cannot retrieve the full path
String fileAttachment = upload.getName();
Server side:
public void sendMail(String sender, String[] recipients, String subject, String message, String fileAttachment) {
try {
...(init)
// Part two is attachment
messageBodyPart = new MimeBodyPart();
// => fileAttachment need full path
DataSource source =
new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(multipart);
// Send
Transport.send(msg);
}
Thanks for your help
You have to actually upload the file to the server.
The easiest way in GWT is to put your FileUpload (and all your form input widgets) in a FormPanel; it has the drawback of making error handling (and response handling from the server) more difficult though.
An alternative, in recent browsers, is to get the File object (not a java.io.File, a JS object) out of the FileUpload and upload it using XMLHttpRequest (possibly coupled with FormData to also send the other form values). In GWT, that means using JSNI (it might be possible to use the Elemental library too), and really know the innards of what you're doing.
In any case, you won't be able to use GWT-RPC to talk to your server and send the file at the same time.
Basically I need to provide REST service that would receive a String param, use that param to fetch a file from another system and then return the fetched file back as the response.
The effect should be the same as when a user clicks on a pdf or any other binary file link and the browser prompts him to save/download that file.
A couple of points:
is it possible to stream the file (to send bytes as I receive them from source system). In other words, how to handle very large files?
also related to streaming, when using regular HttpServletResponse, do I have to wait until a large file is completely read to return response.build()?
How do I go around doing this using Apache Wink?
PS Sorry, this may be trivial for Wink gurus, but I'm just starting to wrap my head around developer guide.
You can just return the java.io.File from your method. You can wrap it with Response if you like. Wink will handle the streaming. The streaming doesn't start when you call to response.build(), but rather when your method finishes.
If you want a correct download dialog, you should return the proper Content-Disposition header. See How to set response header in JAX-RS so that user sees download popup for Excel?
I'm attempting to build a Tropo Ruby application and I need to retrieve the result of an HTTPS GET. The Tropo platform doesn't have the httpclient Ruby gem so I can't use that. The Ruby engine used is JRuby so a suggestion has been to make use of the Java URL class to do the request. I've played around with it a little bit and I seem to be able to create the URL object ok but am now struggling with how to get the results of executing the request. How do I do it?
javaURL = java.net.URL.new svcURL
transferResult = javaURL.getContent()
I am not sure what getContent() does exactly, but you can use openStream() which gives you an InputStream to read from.
javaURL = java.net.URL.new svcURL
transfer = javaURL.openStream()
// read from stream
transfer.close()
I am trying to design an application that would require me to retrieve data stored in blobstore and send it as attachment. Does google app engine allow this? From the documentation, i could not find a way to retrieve data from blobstore for processing within the app.. can someone please tell me how to accomplish this? Code examples and/or pointers to related online resources would be really helpful.
You can now read data from the blobstore, using BlobReader, which provides a simple, file-like interface.
As of now, it seems this isn't possible. You can only cause the the file to be sent to the client.
It's possible you could do what you need using a Datastore Blob?
It's worth also noting that the Blobstore is "experimental" and may change. It's possible additional functionality may be added that would allow what you'r trying to do.
This can be accomplished in two steps using the code from the Complete Sample App.
http://code.google.com/appengine/docs/java/blobstore/overview.html#Complete_Sample_App
1) Write a servlet that takes a blobkey and returns the contents of the blob.
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
blobstoreService.serve(blobKey, res);
}
2) Within your application, use the URLFetchService.fetch(java.net.URL url) with the proper blobkey to retrieve the blob (as a stream) and attach it to the email.
You can use BlobstoreInputStream to do almost the same thing as BlobReader do.
https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/blobstore/BlobstoreInputStream
BlobstoreInputStream provides an InputStream view of a blob in Blobstore. It is thread compatible but not thread safe: there is no static state, but any multithreaded use must be externally synchronized.
I have report in my jsp page and I am writing that report in PDF Format.
And I want to send the PDF as E-Mail with attachment, but I don't want store the file in local machine or server, but i want to send an email with the attachment.
If you use Spring's JavaMail API, you can do this sort of thing fairly easily (or at least, as easily as the JavaMail API allows, which isn't much). So you could write something like this:
JavaMailSenderImpl mailSender = ... instantiate and configure JavaMailSenderImpl here
final byte[] data = .... this holds my PDF data
mailSender.send(new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
// set from, to, subject using helper
helper.addAttachment("my.pdf", new ByteArrayResource(data));
}
});
The attachment data can be any of Spring's Resource abstractions, ByteArrayResource is just one of them.
Note that this part of the Spring API stands on its own, it does not require (but does benefit from) the Spring container.
Since JavaMail 1.4 - mail.jar - contains javax.mail.util.ByteArrayDataSource
https://javamail.java.net/nonav/docs/api/javax/mail/util/ByteArrayDataSource.html
( https://javamail.java.net/nonav/docs/api/ )
http://www.oracle.com/technetwork/java/javamail/index-138643.html - download location
regards
You have to write your own implementation of javax.activation.DataSource to read the attachment data from an memory instead of using one of the included implementations (to read from a file, a URL, etc.). If you have the PDF report in a byte array, you can implement a DataSource which returns the byte array wrapped in a ByteArrayOutputStream.