Archive

Archive for the ‘ASP’ Category

Content-Disposition attachment vs inline

May 30, 2008 13 comments

Today I ran into an interesting issue. We have some legacy code in .NET 1.1 that exports an HTML table to Microsoft Excel. This export occurs by simply rendering the table via Response.Write and setting the header content-disposition to “attachment; filename=FileName.xls”. The original code looked something like this:

  Response.Clear()
  Response.AddHeader("content-disposition", "attachment;filename=SalesByProductReport.xls")
  Response.Charset = "utf-8"
  Response.Cache.SetCacheability(HttpCacheability.NoCache)
  Response.ContentType = "application/vnd.ms-excel"

  Dim stringWrite As IO.StringWriter = New System.IO.StringWriter
  Dim htmlWrite As HtmlTextWriter = New HtmlTextWriter(stringWrite)

  tblTable.RenderControl(htmlWrite)

  Response.Write(stringWrite.ToString())
  Response.Flush()
  Response.End()

The problem that occurred was that any user using Internet Explorer (surprise, surprise!) would get a prompt to download the file but the file would not download! The file worked properly in all other browsers. The solution is to change

Response.AddHeader("content-disposition", "attachment;filename=SalesByProductReport.xls")

to

Response.AddHeader("content-disposition", "inline;filename=SalesByProductReport.xls")

Now, why exactly does this work? I’m not sure, so if you know please tell me.

Advertisements
Categories: .NET, ASP

ASP .NET Nested Forms (Or why do I get the error ‘Invalid postack or callback argument’)

March 7, 2008 5 comments

I have now come across a few situations where using nested forms in ASP .NET causes problems. The typical error produced in this case is a ‘Invalid postback or callback argument’ error. This occurs because ASP .NET allows the rendering of multiple, nested forms but fails to validate the forms when a postback occurs. Thus when ASP .NET recognizes nested forms in a page, it marks the page as not valid (Page.IsValid returns false).

The reason that this error occurs is because multiple, nested forms cannot reside on a single aspx page .

The solution is very simple: The submit button for the user created form must contain the following attribute: onclick=”this.form.submit();”. A recent example I came across is below. The basic form that was developed and placed inside an aspx page as a nested form was as follows (only the beginning and ending of this form is shown. The content is not necessary or relevant):

<form accept-charset=”UTF-8″ action=”http://www.response-o-matic.com/mail.php&#8221; method=”post” enctype=”multipart/form-data”>

                <table>

                                <tbody>

                                                .

                                                .

                                                .

                                                .

                                                <tr>

                                                                <td colspan=”2″ align=”center”>

                                                                                <input value=” Submit Form ” type=”submit” />

                                                                </td>

                                                </tr>

                                </tbody>

                </table>

</form>

The following code demonstrates the proper way of entering this form so that nested forms will work. Please note that I reiterated all of the attributes (action, method, enctype, etc…) of the form in the JavaScript call.

<form accept-charset=”UTF-8″ action=”http://www.response-o-matic.com/mail.php&#8221; method=”post” enctype=”multipart/form-data”>

                <table>

                                <tbody>

                                                .

                                                .

                                                .

                                                .

                                                <tr>

                                                                <td colspan=”2″ align=”center”>

                                                                                <input onclick=”this.form.action=’http://www.response-o-matic.com/mail.php&#8217;; this.form.method=’post’;this.form.enctype=’multipart/form-data’;this.form.submit();” value=” Submit Form ” type=”submit” />

                                                                </td>

                                                </tr>

                                </tbody>

                </table>

</form>

Categories: .NET, ASP

Upload Large Files in ASP.NET

December 3, 2007 Leave a comment

Here’s the simple solution to uploading large files in ASP .net.

Add a new key in the web.config. The fileSize (maxRequestLength) is in kilobytes with a default size of 4.

The key to add is

<httpRuntime maxRequestLength=”2000000″/>

Categories: .NET, ASP, File