0

I'm trying to implement a functionality that enables a user to download a PDF on clicking on a hyper-link. What i've done is, I've created a global datatype Publications which takes values "Description" and "PDF DOC" and I've a user control with a hyper-link which binds the description as its text.

LinkButton1.Text = details.Description;
Composite.Data.DataReference<IMediaFile> i = new Composite.Data.DataReference<IMediaFile>((details as A.DataTypes.Publications).PdfDoc);
string filePath = "/media(" + i.Data.Id + ")";

and on the click on the link button I've...

Response.ContentType = "Application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=Test_PDF.pdf");
Response.TransmitFile(filePath );
Response.End();

this is showing an error saying "could not find file", any idea why?

orique
  • 1,295
  • 1
  • 27
  • 36
Rosh
  • 21
  • 7

4 Answers4

0

It looks like you are trying to use the C1 syntax for media files at a place where the C1 page renderer never replaces it with the actual url of the file. So you end up passing something like /media(b5354eba-3f69-4885-9eba-74576dff372d) to the Response.TransmitFile() function, which will not work because that is not a valid file path.

If you use this syntax on a C1 page, the page renderer will replace it with the real url of the file.

My advise would be to build this URL yourself and just link to it, instead of using TransmitFile. A simple redirect will suffice if the file is open for public access. If it is lying acessible on the web server already, there is not much point in using Response.TransmitFile and fetching it and writing it in the outputstream.

Community
  • 1
  • 1
magnattic
  • 12,638
  • 13
  • 62
  • 115
  • I've tried passing "/media(b5354eba-3f69-4885-9eba-74576dff372d)" to the GetMediaUrl() in the link that you've suggested but still not getting the url as return value..moreover there is no ':' in the media path that i'm sending to split...is there something else I need to do?? btwn thanks for the quick response and sry for the trouble!! – Rosh Jun 04 '13 at 05:14
  • hey I'm in a really bad situation here..could you plsss help? – Rosh Jun 05 '13 at 04:18
0

Try look at the DownloadFoldersAsZip package (https://bitbucket.org/burningice/compositec1contrib/src/4c31794cd46c/DownloadFoldersAsZip?at=default) which has this functionality. The main issue with your code is that you make the assumption of where the files are stored. You can't do that with the C1 Media Archive, since files can be either local, in a database, in Azure Blob storage or just a random place on the internet.

Instead you should use the GetReadStream() extension method of your IMediaFile instance. This will give you a stream which you can copy unto your Response.ResponseStream

See here for an example: https://bitbucket.org/burningice/compositec1contrib/src/4c31794cd46cb03dd0b0ee830b83204753355b03/DownloadFoldersAsZip/Web/GenerateZipHandler.cs?at=default#cl-145

Pauli Østerø
  • 6,878
  • 2
  • 31
  • 48
0

solved it, just needed to give....string filePath = "~/media({" + i.Data.Id + "})"; instead of string filePath = "/media(" + i.Data.Id + ")";

Rosh
  • 21
  • 7
0

You can also use this code

    Composite.Data.DataReference i = new Composite.Data.DataReference((details as A.DataTypes.Publications).PdfDoc)

This gives the media file reference

    string fileName = "/App_Data/Media/" + i.Data.Id.ToString();
    this.Response.AddHeader("content-disposition", string.Format(
       "attachment;filename=download.pdf", Path.GetFileName(fileName)));
    this.Response.ContentType = "application/pdf";
    this.Response.WriteFile(this.Server.MapPath(fileName));

This can get the file Downloaded as download.pdf