November Happy Hour will be moved to Thursday December 5th.

Save Target As for Video Files in a VPP.

Vote:
 

Hi Guys,

I'm needing to have the "Save File Dialog" show up when a user clicks on a "Download" button for a video. I've currently got a Download page which expects an ID to then - in code - grab the PageReference of the ID and get the pagedata back and to a link to a virtual path file.

The actual address I get back is "/Documents/folder/folder/video.mov". Since I will need to then take the contents of this file and put them in the output stream, I will need to grab the actual file data. Since this file uses the Virtual Path Provider, when I use Server.MapPath() all I get back is it trying to go to my local machine and not the Virtual Path. How can I get back the actual video data in code?

Cheers.

#33418
Oct 12, 2009 14:52
Vote:
 

Hi,

 If i've understood you correct you can use this code where "file" is the path to your movie: 

UnifiedFile filePath = HostingEnvironment.VirtualPathProvider.GetFile(file) as UnifiedFile;
if (filePath == null) return;

Stream s = filePath.Open(FileMode.Open, FileAccess.Read);
int len = (int)s.Length;
byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, len);
s.Close();
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=" + filePath.Name);
Response.BinaryWrite(buffer);
Response.End();
 

-Kjetil 

#33422
Oct 12, 2009 15:27
Vote:
 

Beware of that this approach might cause some massive amount of memory to be allocated...consider for example sending a 1Gb videofile which would cause a 1Gb buffer to be allocated...

Now, all this is done for you in the EPiServer.Web.Hosting.WebDownloadManager which has a convenient method: TransmitFile.

You might call it like this:
EPiServer.Web.Hosting.WebDownloadManager,TransmitFile("/Documents/folder/folder/video.mov")

If the file is hosted on a nativeprovider, the fastes Response.TransmitFile can be used, if not, the file content is streamed in reasonably sized chunks.

It also sets the appropriate content headers for you if you specify the DownloadAction.Download
(i.e. content-disposition: attachment)

/johan

 

#33442
Oct 13, 2009 15:16
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.