4

I am trying to download a file from Google Docs, via my ASP.Net app.

Since I am using OAuth, I generated a signature and into authorization string.

From Google Docs documentation, to access it's resource, I will need to add:

Authorization: <authorization string>

in the request header.

I wrote a snippet to do the redirection:

context.Response.AddHeader("GData-Version", "3.0");
context.Response.AddHeader("Host", downloadLinkUrl.Host);
context.Response.AddHeader("Authorization", authorizaString);
context.Response.RedirectLocation = downloadLink;
context.Response.Redirect(downloadLink);

It redirected to the downloadLink, but Header information is missing, as seen from Firebug. Thus I get 401 Not Authorized.

Some read up said that it's not possible. Is there any hack around?

Thanks.

Community
  • 1
  • 1
VHanded
  • 2,079
  • 4
  • 30
  • 55

2 Answers2

2

You cannot forward a header. Your only bet is to try to use jQuery/javascript on a redirect page to add the headers and then make a GET request. I'm not even sure if you can since AJAX requests cant be used for file download.

Actually.. I don't think that will work either. So - I have to say no, you can't do this for a file download. However can you make the request from the server and then stream it to the client?

EDIT I answered this in the past: jQuery delivery of a file with AJAX call

Since jQuery get is a shorthand form of .ajax - this MAY work by doing a jQuery ajax call specifying 'get'. Give it a try and let us know the results.

http://api.jquery.com/jQuery.ajax#options

For some code see: Pass request headers in a jQuery AJAX GET call

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • That was innitial plan, but it will take double the time to stream the file from Google Docs to my server, then from my server to client browser. – VHanded Sep 14 '11 at 04:25
  • After 5 hours trying, the jQuery method works if I request my own site, but unable to request other site, which is Google Docs, in my case, due to something call 'same origin policy'. Any other workaround? – VHanded Sep 14 '11 at 10:43
  • ah cr@ppers. sorry. I wasn't thinking about the cross domain call but yes, this is a valid issue. I'm afraid the only workaround I can think of is to actually stream down locally, then to the client. There's no other way the client can inject these headers. They could request from your site through an asyn. page request to help free up IIS from the long processing time. Your handler would in turn download the file and stream it to the client. The other option is to investigate if you can add a token to the URL or a cookie that google docs can use. – Adam Tuliper Sep 14 '11 at 16:23
  • It's ok, you had given me enough help. I am currently download to server, then stream to client browser, with some javascript changing the download button to 'preparing download' to cope with the extra waiting time. Thanks for your help. – VHanded Sep 15 '11 at 04:22
0

In express.js there is possible to redirect response with both headers and cookies:

res.set('mykey', 'myvalue');
res.set('Access-Control-Expose-Headers', 'Set-Cookie, mykey');
res.cookie('mykey', 'myvalue', { maxAge: 900000, httpOnly: true });
res.redirect(302, url);

I believe in .NET there is something similar.

MMM
  • 103
  • 13