Ajax.ActionLink download file

  • Thread starter Thread starter Guest 20221009
  • Start date Start date
G

Guest 20221009

Guest
I got this code that is supposed to trigger a file download.

@Ajax.ActionLink("Proceed", "DownLoad", "Video", new AjaxOptions { HttpMethod = "Get", OnSuccess = "closeMessage", LoadingElementDuration= 5, OnBegin = "resizeLightbox()" })

The only problem is that it does this in the back ground and I cant see where the downloads are going. I only noticed that the thing was actually processing by looking at the network activity tab on Chrome.

my headers are like:


Response

X-Requested-With:XMLHttpRequest
Response Headersview source
Cache-Control:private
Connection:Close
Content-Disposition:attachment; filename=Denkowmv
Content-Length:57191049
Content-Type:video/x-ms-wmv
Date:Thu, 29 Sep 2011 14:14:06 GMT
Server:ASP.NET Development Server/10.0.0.0
Set-Cookie:"-censored-" GMT; path=/
dctoken=B2344621020F1757; domain=xxxxx.com; expires=Sat, 29-Oct-2011 14:14:05 GMT; path=/
dctoken_time=9/29/2011 4:14:05 PM; domain=dstv.com; expires=Sat, 29-Oct-2011 14:14:05 GMT; path=/
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0

We need the ajax call for user experience, and a normal action link does the trick.Any help is most welcome. :cry::cry:
 
Last edited by a moderator:
You're using JavaScript you didn't include here. Without seeing that code, I won't be able to tell you where your downloads are going. There must be some form of server-side scripting to go along with that too.

You work for dstv?
 
You're using JavaScript you didn't include here. Without seeing that code, I won't be able to tell you where your downloads are going. There must be some form of server-side scripting to go along with that too.

You work for dstv?


I am using MVC's built in Ajax functions.
And yeah, I have been working at DStv Online for the past three months, on contract though. Some of the code below might be a bit messy.. :o:o

public EmptyResult DownLoad(int videoid, int program)
{

// Removed


return null;
}


The download function:

//System.IO.Stream iStream = null;
Stream iStream = null;

// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];

// Length of the file:
int length;

// Total bytes to read:
long dataToRead;



try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();


iStream = resp.GetResponseStream();

dataToRead = resp.ContentLength;

string filename = string.Format("{0}.wmv", file); ;//System.IO.Path.GetFileName(filepath);

Response.ContentType = resp.ContentType;
Response.AddHeader("Content-Disposition", "attachment; filename=" + VideoHelpers.Fixfunction(filename));
Response.AddHeader("Content-Length", dataToRead.ToString());

// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);

// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);

// Flush the data to the HTML output.
Response.Flush();

buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
log.Error(ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
Response.Close();
}
}
 
Last edited by a moderator:
I found this work around:

http://stackoverflow.com/questions/5694399/display-inline-document-on-partial-page-on-ajax-action-link

The link below explains better:

AJAX makes this somewhat difficult. Since there is no traditional HTTP response, you have no context with which to send the file to the browser for normal download.

http://encosia.com/ajax-file-downloads-and-iframes/

Which probably explains why I could still trace the download through firebug and fiddler but could not locate the wmv at the end of the download. I need to brush up on my HTTP stuff :o... /grabs a copy of, HTTP: The definative guide
 
Sweet jesus WTF is with all those try catches all over the place. Perhaps stop swallowing your exceptions you might also get some errors that may be there that you didnt realize.

But on a personal note if you worked for me id make you rewrite all that ****.:D
 
Sweet jesus WTF is with all those try catches all over the place. Perhaps stop swallowing your exceptions you might also get some errors that may be there that you didnt realize.

But on a personal note if you worked for me id make you rewrite all that ****.:D

I agree, and I did warn about the quality and yep, I have thoroughly cleaned in it up :D
 
Top
Sign up to the MyBroadband newsletter
X