open document in browser without generating file in server side - google-app-engine

I have stored user document (ms word) in data store as blob object.
How do I open this in a browser? I can make it downloadable but I want user to view the document in browser before downloading it. how do I do that?
I think, to be able to open the file in browser by setting response headers (inline and content-type), the file should physically exist on a file location.
I don't have space to generate the file and then write to the browser. How do I handle it?
can I store them in google drive? does reading and uploading will be charged? any pointer to examples?
daoIntf=new DAOImpl();
document = daoIntf.getEntity("Document", id);
Blob docBlob=(Blob)document.getProperty("resume");
String fileName=(String)document.getProperty("fileName");
String contentType=(String)document.getProperty("contentType");
response.setContentType(contentType);
response.setHeader("Pragma" , "no-cache");
response.setHeader("Cache-Control" , "no-cache");
response.setDateHeader("Expires" , 0);
response.setHeader("Content-Disposition" , "inline;filename=\""+fileName+"\"");
ServletOutputStream out = response.getOutputStream();
out.write(docBlob.getBytes());
-thanks
Ma

If you have a document in the Blobstore, you simply need to provide a link to it (i.e. .getServingUrl() in Java). When a user clicks on that link, his browser will either offer to download it, or it will open it in the preview mode if this user has a plugin or extension that can do it.

There is an online Google Docs viewer that works on external documents: https://docs.google.com/a/iddiction.com/viewer?pli=1
Expose your documents via servlet using blobstoreService.serve(key, response) and then open them in the viewer.
http://docs.google.com/viewer?url=<url_of_your_document>&embedded=<true|false>

Related

Streaming the zip file to the calling application using Vert.x

One Gateway application is going to send a GET request to my Vert.x application. For this request, I need to read a large zip file from Amazon S3 server which I am able to read in BufferedInputStream. I don't want to download this file but rather I need to send this stream data to the gateway application (NOT a downloadable file with application/zip content type but stream data or byte chunks) which it will further send to end application where this stream data will be downloaded as a zip file. So the sending the zip file in form of stream to the gateway application I need to achieve using Vert.x. Already gone through lot of documentation and blogs but everywhere it is given to download the zip file which is not my intention. Could anyone please suggest how I could achieve this streaming of zip file in http response of calling request using Vert.x? Do I need to use Java NIO? If yes, could you please put details? Sorry, but I have nothing to put here as part of code. Thanks in advance!

Google Cloud Storage Force Download

I am storing my files in Google Cloud Storage. I would like to provide downloadable links. For example https://yeketakclub.storage.googleapis.com/audios/yeketak.club-dm7aEYv7R53JRlti3HHn.mp3 one of audio files stored in google cloud storage. But when it is clicked browser tries to open it. Is it possible to force download?
The correct answer is neither of these! (if you don't want to edit a file's metadata) Add this on the end of any signed url:
&response-content-disposition=attachment;
This will make all storage links force a download instead of opening.
You can signal browsers to download the object while still retaining an accurate content type by setting the content disposition to attachment. For, example using gsutil you can do this like so:
gsutil setmeta -h 'Content-Disposition:attachment' gs://yeketakclub/audios/yeketak.club-dm7aEYv7R53JRlti3HHn.mp3
Now your object can still have the correct content type of "audio/mpeg3" (or whatever happens to match the object's content).
I'm not sure if this is necessarily a Google Cloud Storage issue (I might be wrong). The link provided there is downloadable. It just happens to be that your browser "prefers" to play it most probably because it recognises the MIME Type as one that can be handled.
In Chrome for instance, you can force download of the file by using alt + click.
Or you can right click and save link as...
In the Google Console bucket area you can click the menu and edit metadata on the object and set the Content-Disposition to attachment
edit metadata -> set Content-Disposition = attachment
image

From Drive to Blobstore using Picker

I have the Google picker set up, as well as Blobstore. I'm able to upload files from my local machine to the Blobstore, but now I have the Picker set up, it works, but I don't know know how to use the info (url? fileid?) to then load that selected file into the Blobstore? Any tips on how to do this? I haven't been able to find much of anything on it on Googles resources
There isn't a direct link between the Google Picker and the App Engine Blobstore. They are kind of different tools for different jobs. The Google Picker is designed as an end user tool, to select data from a users Google account. It just so happens that the Picker also provides an upload interface (to Google Drive) as well. The Blobstore on the other hand, is designed as a blob storage mechanism for your App Engine application.
In theory, you could write a script to connect the two, but there are a few considerations:
Your app would need access to the users Google Drive account using OAuth2. This is necessary, as the Picker API is a client side API, whereas the Blobstore API is a server side API. You would need to send the selected document URL to the server, then download the document and finally save it to Blobstore.
Unless you then deleted the data from Drive (very risky due to point 3), your data would be persisted in 2 places
You cannot know for sure if the user selected an existing file, or uploaded a new one
Not a great user experience - the user things they are uploading to Drive
In essence, this sounds like a bad idea! What is your use case?
#Gwyn - I don't have enough reputation to add a comment to your solution, but I had an idea about problem #3: You cannot know for sure if the user selected an existing file, or uploaded a new one
Would it be possible to use Response.VIEW to see what view they were using when the file was selected? If you have one view constructor for Drive files and one for Upload files, something like
var driveView = new google.picker.View(google.picker.ViewId.DOCS);
var uploadView = new google.picker.DocsUploadView();
would that allow you to know whether the file was a new upload (safe to delete) or an existing file (leave it alone)?
Assuming that you want to pick a file from your own Google Drive and move it to the Blobstore.
1)First you have to perform Oauth for Google Drive API
2)Using the picker when you select a file from drive, you need to get it's id
3)Using the id obtained in step 2 you can programmatically download it using Drive API
4)After downloading the file you can use FileService(deprecated though) to upload the file to the
Blobstore.

How do i determine the stream size from an uploaded file from a website which i want to insert in Google Drive

I'm trying to upload files to Google Drive with ProgressListener and ChunkSize enabled (thus with DirectUploadEnabled disabled). This way i have a more reliable upload and the possibility for a progress indication to the user.
I transfer the files from the GWT website to the GAE with a FormPanel and a FileUploadField which POSTS the file to GAE on submit(). On the GAE i receive the file with an UploadServlet which uses org.apache.commons.fileupload to receive the documents as a stream. I don't want to receive the complete documents on the GAE because the documents are to big. Therefore i start the upload (insert) to Google Drive with the received stream from the incoming request.
Now there's a problem; for the insert i need to know the size of the stream;
int lContentLength = getRequest().getContentLength();
FileItemStream lFileItemStream = getFileItemStream();
InputStream lInputStream = lFileItemStream.openStream();
BufferedInputStream lBufferedInputStream = new BufferedInputStream(lInputStream);
InputStreamContent lInputStreamContent = new InputStreamContent(pContentType, lBufferedInputStream);
lInputStreamContent.setLength(lContentLength);
My first guess was the ContentLengt from the incoming Servlet request. But this is not correct because this concerns the complete request (which also contains other fields which are used as parameters). Without the Drive option DirectUploadEnabled i need the exact stream size from the uploaded document, otherwise the upload stall's at the end...
How do i grap this document size? The Google example is stupid because it uses a local file;
https://code.google.com/p/google-api-java-client/wiki/MediaUpload
Yes from a local file it is easy to get the file size (mediaFile.length()). But from a website ... Several sites specify it is not possible to grab the file size before submit() from the website, and it seems also impossible to determine the stream-size on GAE without loading the complete file...
How do i determine this streamsize? Is there another solution for this problem?

Is it possible to read files stored on a computer?

I want to access a file in computer(c:\test.bin) and to read it as byte array .Is it possible in Windows phone .
Thanks and Regards
vaysage
You cannot access a file via any standard file I/O apis.
You can run a web server on that computer, make the file available via http and include the appropriate client access policy file in the web site. You can then download the file via WebClient using OpenReadAsync.
If you would like to upload something as a project asset, then you should force it to be a "content" from the properties panel and then access it using :
Uri uriMyFile = new Uri("test.bin",UriKind.relative);
StreamReader sr = new StreamReader((Application.GetResourcesStream(
uriMyFile)).Stream);

Resources