pdf.js how to pass a file response object in c# - c#

so I am using the excellent pdf.js tool and it works great. However, I'd like to pass it a response object that has the PDF file stream from Amazon S3 over to the viewer of pdf.js.
In the demo I see it calls it like this:
=/pdf/web/viewer.html?file=%2FmypdfFile.pdf
However, looking in viewer.html or pdf.js or any of its files, I cannot see where on earth its using the ?file parameter that is passed on the URL. I'd like to replace it with something where I can pass it a response item and it will load up the viewer.html.
I'd like to do something like this:
(pseudo code sorta)
request = S3.GetObjectRequest(bucket, key);
using GetObjectResponse response - client.getobject(request);
openPDFViewer (response);
Is that doable? response would contain the file, i.e. I can say
response.WriteResponseToFile("c:\mypdf.pdf")
and I get the file out.

I cannot see where on earth its using the ?file parameter that is
passed on the URL. I'd like to replace it with something where I can
pass it a response item and it will load up the viewer.html.
If you look into viewer.js, there is this pdfViewOpen method having following parameter:-
pdfViewOpen(url, id, scale, password,pdfDataRangeTransport, args)
The string url that you pass after file= is passed in as url parameter of this method. You can change inside that method what u want to do with it.
From there you might want to look at PDFJS.getDocument() method of pdf.js file, this follows to fetchDocument method from where its taken care by messageHandler.
If you want to handle all that by yourself, you can intercept it before. There are already several examples on SO. Here is link to one:-
How to get byte-array data from servlet to pdf.js

IIRC S3 files have URL references so you don't have to 'preload' the pdf on the server side. Just render out the URL to have the file=[urlencoded path to s3 file]

Related

How to get a raw json as a parameter

I wanna get a raw json string from my client in my WebAPI application.
I tried it like this :
public string Get([FromBody]string rawjson)
{
}
I use Google Chrome Rest Console to try my methods first. I add my json content to RAW body and then send a get request.I put a breakpoint on my method to see if i can get the raw json data however, method invokes but rawjson comes as null. I've also tried put but that didn't work either.
What am i doing wrong ?
Thanks
GET methods cannot have Request Body and as such cannot parse values using [FromBody]. Please use the POST method.
Change the rawJson parameter type to Stream you will recieve anything that is posted on your service as as stream and than you can just read that stream to string

Using dropzone need to add additional values

I am using dropzone to upload files and images to the DB which works perfect, I generate the dropzone div's and call the dropzone jquery function.
In C# its been received by a WebMethod and files are being uploaded to the database.
Now I need to send several id with however I would like to avoid to implement a ajax call to send these id's. After read the documentation on dropzonejs I could not find a simple solution to do this.
My WebMethod does not accept parameter for now but when I have a good way to implement this on client I can add these to WebMethod.
Did I miss something or Am I only able to do this with ajax?
In short looking for the "data:" object within dropzone as in ajax
I am not sure if I understood your question correctly, the formData is available before each files are sent.
From Dropzone.js. Sending: Called just before each file is sent. Gets the xhr object and the formData objects as second and third parameters, so you can modify them (for example to add a CSRF token) or add additional data.
Sample usage:
sending: function(file, xhr, formData) {
formData.append("test",$('#test').val());
},

Setting up application to download a file in asp.net MVC - Nothing happening on return from download

I realize that variations on this question have been asked before. The best answer I have found is at
File download in Asp.Net MVC 2
But trying to follow those instructions did not solve the problem for me.
Long story short, the file is being retrieved correctly, the name, path and mime type are all correct, and no errors are thrown. No errors are thrown by the javascript on the client-side either.
The C# code that gets the file looks like this:
[HttpPost]
public FileResult DownloadFile(int fileId)
{
... get the file and file info
return File(fileBytes, fileMimeType, fileName);
}
The javascript looks like this:
... set up for post here
$.post(settings.actions.downloadFile, {fileId: fileIdVar});
As I was saying, the post returns and nothing happens.
I have tried changing the post to a get, and the result was the same.
I have tried setting up a callback function that sets document.location.href to some random url on return from the download, but that just takes my browser to the page I specified. I cannot understand, from the explanation given in the link I provided, that is
"...Use document.location.href = ... to tell the browser to go to the url for downloading the file. It'll see the content disposition header and will display it as a download not as a page..."
What I'm supposed to point my browser to. document.location.href doesn't accept data, so I can't use it on its own, and using post without document.location.href returns nothing.
What could I be doing wrong?
Big thanks to responders for their time!
Just like the answer in the post you linked says, you can't download a file via AJAX.
In order to set the location, change your action to respond to GET requests and either add your file id to the query string or setup a route to handle it. Also, and you may already be doing this, but you'll need to make sure you set the Content-Dispostion header value to attachment.
window.location.href = settings.action.downloadFile + "?fileId=" + fileIdVar
Since you are using jQuery, you could use $.param to build the parameters for you.
You could also look into a plugin to provide an "AJAX like" experience.

Calling C# from JavaScript with parameters

I'm using source files found at http://www.thecssninja.com/javascript/gmail-upload (all credit goes to CSSNinja for the code). And what I'm having an issue with is I basically need to convert a section of this JavaScript to interface with C#, the problem is i really don't know JavaScript. I need to be able to save the files to specific directories with specific file names based on Session variables. Current the script calls a simple php, and I don't know how to use PHP to interface with Sessions. Any help would be awesome.
xhr.open("POST", "upload.php");
xhr.setRequestHeader("If-Modified-Since", "Mon, 26 Jul 1997 05:00:00 GMT");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("X-File-Name", file.fileName);
xhr.setRequestHeader("X-File-Size", file.fileSize);
xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.send(file);
The question is basically how to convert the above to interface with a class in C#, passing in any/one parameter that is above.
PHP code
<?php
$headers = getallheaders();
// create the object and assign property
$file = new stdClass;
$file->name = basename($headers['X-File-Name']);
$file->size = $headers['X-File-Size'];
$file->content = file_get_contents("php://input");
// if everything is ok, save the file somewhere
echo $file->content;
?>
All right, enough comments...
All that JavaScript is doing is sending you two special HTTP headers and a file stream as the request body, and the PHP is reading that. Nothing more.
setRequestHeader(name, value) sets a normal HTTP header with the name name and the value value, while the file content is sent as a normal request body. All standard HTTP.
If you know your C# (and presumably ASP.NET), accessing two HTTP headers and reading the request stream should be no problem.

How is an image served from a URL with an ASPX extension?

Can Somebody tell how to create such kind of urls
for example if you see the url
http://office.microsoft.com/global/images/default.aspx?assetid=ZA103873861033
you will redirect to an image ..
my question is , though this url is an image..its extension is aspx..how is it possible.
how to create such kind of url's
Thanks
This is a common method for displaying an image that's stored as a binary object in a database. One tutorial, among many, can be found here.
Essentially, what they're doing is using the aspx page to accept the URL parameter which tells them what image to fetch from the database. Then in the response they clear all output and headers, set the headers for the image, write the binary data to the response stream, and close the response stream.
So it's not really "redirecting" you to an image. The "page" being requested turns out to be an image resource in the response.
By setting the ContentType in the response from the server
HttpContext.Response.ContentType = "image/jpeg";
easiest way is to add generic handler *.ashx and in ashx file u'll have code behind which u can get querystring and manipulate response eg. Response.WriteFile(...)
File extensions literally have no meaning on the WWW. The thing that correctly describes the content at a particular URL is the content-type/MIME-type. This is delivered in an HTTP header when the URL is requested prior to delivery of the main HTTP payload. Other answers describe how you might correctly set this in ASP.NET.
Aside from all other answers they may be doing a Server.Transfer() (so that you don't see it client-side) to the image file. This still means the response headers are being set to the appropriate MIME type but it also means the image isn't necesarilly coming from a database. This technique can be used to hide the actual image URL in attempts to prevent hotlinking.

Categories

Resources