Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesResponse.WriteFile problemResponse.WriteFile problem
Previous
 
Next
New Post
2/16/2011 9:04 AM
 
Good day.

I'm developing a DNN module. One of the functionality, is that of export content to a .csv file, but I have found quite a BIG problem. Here's a sample code:


Response.AddHeader("Content-Disposition", "attachment; filename=file.csv")
Response.ContentType = "text/csv"
Response.WriteFile(Server.MapPath("file.csv"))
Response.End()


note: The code is called during a postback, after the user clicks a button

Almost everything works fine, except that the downloaded file contains the ENTIRE HTML PAGE CONTENT, appended at the end of the file. I noticed this behaviour with any type of file (for examples, .pdf files can be opened and viewed, but if opened with a text-editor, i can see the page html code)

I tried the exact same code outside a dnn module, in a "testfile.aspx" page, and everything works fine.

Is there a way to solve this? Am I missing something?

Any help would be appreciated. If you need more informations please ask.

Thanks in advance,

Alberto.
 
New Post
2/16/2011 9:20 AM
 
You will need to add the following lines at the beginning of your code:

Response.ClearContent()
Response.ClearHeaders()

I would, however, suggest looking into making use of the core's LinkClickHandler or one of the DotNetNuke.Common.Utilities.FileSystemUtils.Download methods as these can take into account the correct handling of DotNetNuke permissions as well as streaming the contents of files located in secure or database secure folders.

Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
 
New Post
2/16/2011 9:59 AM
 
Thank you for the quick response.

Unfortunately, adding the ClearContent and ClearHeaders doesn't help.

I will give a look to DotNetNuke.Common.Utilities.FileSystemUtils.Download, but I fear that I'll NEED to solve this problem.

I tried the response.write method for this reason:

What I want to achieve is download the file without actually saving it in the server, but dinamically creating in a string and then using response.binarywrite()..
My module already do everything (the code posted before was just an example) but I need to "trunk" the response content after the response.binarywrite(), in order to avoid the page content to be appended to the file.

A tried with response.close(), response.flush(), response.end() just after the response.binary.write(), but nothing changes.

The fact is that, in another module, I'm dinamically creating a pdf using iTextSharp libraries, putting them into a memorystream and directly outputting them to the user.. actually, the pdf is generated with the "appended html content". Since the content of the file is constantly updated, I prefer not to save them in the server, otherwise I will have to:

-save the file with a unique name (in order to avoid simultaneous access to the same file name)
-show the open/save message to the user
-delete the above generated file

But it doesn't seem a good solution to me (maybe I am wrong)

Any other suggestion/solution is welcome! 

Thank's,

Alberto
 
New Post
2/16/2011 1:28 PM
 
You don't need to save the file on the server. Any in memory object can be written to a stream. Here is a simple example I just put together that works.
        string txt = "Here is some generic text!";
        
        UTF8Encoding utf8 = new UTF8Encoding();
        byte[] buf = utf8.GetBytes(txt);        
        
        Response.ContentType = "text/plain";
        Response.AppendHeader("content-disposition", "attachment; filename=file.txt");

        Response.BinaryWrite(buf);
        Response.End();       

My browser downloads this as .txt file without my code having to first save it to the server.

Something to keep in mind are the valid http mime types. I don't think text/csv is a valid mime type. Here is a list of mime types:
http://www.w3schools.com/media/media_mimeref.asp.

It should work the same whether in a module or a stand alone aspx page.
       
 
New Post
2/16/2011 1:42 PM
 
In re-reading your first post I see that I originally mis-read that the page content was being appended at the end of your pdf document not at the beginning.

Several years ago I wrote an ASP.Net server control which uses iTextSharp to dynamically generate a pdf from database content and have used it on a number of DNN sites with good success. Here's the code for the method which streams the pdf content:

'Sends the PDF document to the client's browser via the HttpResponse stream.
        Public Sub StreamPDF(ByVal Response As System.Web.HttpResponse, ByVal OpenNewWindow As Boolean, ByVal OutputFilePath As String)
            If Not Response Is Nothing And Not PDFStream Is Nothing Then
                If DocumentIsOpen Then
                    PDFDoc.Close()
                End If
                PDFStream.Flush()
                PDFStream.Close()
                Dim Buffer As Byte() = CType(PDFStream, MemoryStream).ToArray()
                Dim BufLen As Integer = Buffer.Length
                Response.Clear()
                Try
                    Response.ClearHeaders()
                Catch ex As Exception
                    'Eat the exception which results from clearing the cookies collection but is
                    'OK in this case
                End Try

                If OutputFilePath Is Nothing OrElse OutputFilePath = String.Empty Then
                    If Title Is Nothing OrElse Title = String.Empty Then
                        OutputFilePath = "Report.pdf"
                    Else
                        OutputFilePath = Title & ".pdf"
                    End If
                Else
                    If Path.GetExtension(OutputFilePath).ToLower <> ".pdf" Then
                        OutputFilePath = Path.GetFileNameWithoutExtension(OutputFilePath) & ".pdf"
                    End If
                End If
                Response.AppendHeader("Content-Length", BufLen.ToString)
                If OpenNewWindow Then
                    Response.AppendHeader("content-disposition", "attachment; filename=" & OutputFilePath)
                Else
                    Response.AppendHeader("content-disposition", "inline; filename=" & OutputFilePath)
                End If
                Response.AppendHeader("content-type", "application/pdf")
                Response.Flush() 'send the header information to the client
                Response.OutputStream.Write(Buffer, 0, BufLen)
                Response.Flush()
                Response.End()
            End If
        End Sub

Note in particular that I am doing a double Response.Flush () - once to ensure that the header is written and a second one after the pdf bytes are written. That may make the difference.


Bill, WESNet Designs
Team Lead - DotNetNuke Gallery Module Project (Not Actively Being Developed)
Extensions Forge Projects . . .
Current: UserExport, ContentDeJour, ePrayer, DNN NewsTicker, By Invitation
Coming Soon: FRBO-For Rent By Owner
 
Previous
 
Next
HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesResponse.WriteFile problemResponse.WriteFile problem


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out