HtmlInputFile corrupts Excel 2003 (C#)

R

Rik Moed

Hi,

I am having a problem with Excel 2003 worksheets when I upload them using
the HtmlIputFile. After the upload, I start to download the worksheet and it
appears to be currupt.

I recieve the error:
<FileName> cannot be accessed. The file may be read-only, or you may be
trying to access a read-only location. Or, the server the document is
stored on may not be responding.

Here I can choose between Retry and Cancel, Retry does not work, Cancel
gives me the following error:
Damage to the file was so extensive that repairs were not possible. Excel
attempted to recover your formulas and values, but some data may have been
lost or corrupted.

Steps to reproduce:

1. Create a new project (ASP. Net Web Application), name it "uploader"
2. Open html view of the generated WebForm1
3. Copy the following code inside the form tag

<input id="Uploader" type="file" runat="server">
<asp:Button Runat="server" ID="UpAndDown" Text="Up and down"></asp:Button>

4. Copy the following code in the codebehind WebForm1.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace uploader
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UpAndDown;
protected System.Web.UI.HtmlControls.HtmlInputFile Uploader;

private void Page_Load(object sender, System.EventArgs e)
{
UpAndDown.Click += new EventHandler(UpAndDown_Click);
}

private void UpAndDown_Click(object sender, EventArgs e)
{
byte [] b = new byte[Uploader.PostedFile.InputStream.Length - 1];

Uploader.PostedFile.InputStream.Read(b, 0, b.Length);
Uploader.PostedFile.InputStream.Close();

Response.Clear();
Response.Buffer = true;
Response.Expires = -1;

Response.AddHeader("Content-disposition", "attachment;
filename=\"download.xls\"");

Response.BinaryWrite(b);
Response.End();

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

5. Run the application

6. Create an Excel 2003 workbook with some dummy data, save it to a
temporary location, name the file Excel2003.xls

7. Create an Excel 2003 workbook with some dummy data, choose Save as-->
save as type: "Microsoft Excel 97", name the file Excel97.xls

8. now use the uploader program to up and download both files. the
excel2003.xls will generate the error described above, the Excel 97 format
will work fine!

Does anybody know a solution to this problem?

Thanks!

Rik Moed
 
P

Patrice

To better diagnose the problem I would rather do something like :
- do your upload page and save the file (for exemple using the Save method).
- do a download page with not HTML markup to see if you can stream the file
to the browser

As you'll be able to open the server side you'll be able to see where the
problem lies (during the upload or the download) and to test the offending
phase separately.

Binary files are binary files but it could be some stream encoding issue ? I
would use httpPostedFile.Save and Response.WriteFile to save/stream the
file...

You could also do a binary comparison of the file you get compared with the
file you posted...


Patrice

--

Rik Moed said:
Hi,

I am having a problem with Excel 2003 worksheets when I upload them using
the HtmlIputFile. After the upload, I start to download the worksheet and it
appears to be currupt.

I recieve the error:
<FileName> cannot be accessed. The file may be read-only, or you may be
trying to access a read-only location. Or, the server the document is
stored on may not be responding.

Here I can choose between Retry and Cancel, Retry does not work, Cancel
gives me the following error:
Damage to the file was so extensive that repairs were not possible. Excel
attempted to recover your formulas and values, but some data may have been
lost or corrupted.

Steps to reproduce:

1. Create a new project (ASP. Net Web Application), name it "uploader"
2. Open html view of the generated WebForm1
3. Copy the following code inside the form tag

<input id="Uploader" type="file" runat="server">
<asp:Button Runat="server" ID="UpAndDown" Text="Up and down"></asp:Button>

4. Copy the following code in the codebehind WebForm1.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace uploader
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UpAndDown;
protected System.Web.UI.HtmlControls.HtmlInputFile Uploader;

private void Page_Load(object sender, System.EventArgs e)
{
UpAndDown.Click += new EventHandler(UpAndDown_Click);
}

private void UpAndDown_Click(object sender, EventArgs e)
{
byte [] b = new byte[Uploader.PostedFile.InputStream.Length - 1];

Uploader.PostedFile.InputStream.Read(b, 0, b.Length);
Uploader.PostedFile.InputStream.Close();

Response.Clear();
Response.Buffer = true;
Response.Expires = -1;

Response.AddHeader("Content-disposition", "attachment;
filename=\"download.xls\"");

Response.BinaryWrite(b);
Response.End();

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

5. Run the application

6. Create an Excel 2003 workbook with some dummy data, save it to a
temporary location, name the file Excel2003.xls

7. Create an Excel 2003 workbook with some dummy data, choose Save as-->
save as type: "Microsoft Excel 97", name the file Excel97.xls

8. now use the uploader program to up and download both files. the
excel2003.xls will generate the error described above, the Excel 97 format
will work fine!

Does anybody know a solution to this problem?

Thanks!

Rik Moed
 
R

Rik Moed

Thanx for your answer. I found the problem using your hint. I compared the
original file with the downloaded file and found out that the downloaded
file is 1 byte smaller.

I don't know why I put this line of code like this:

byte [] b = new byte[Uploader.PostedFile.InputStream.Length - 1];

But when i changed i to:

byte [] b = new byte[Uploader.PostedFile.InputStream.Length];

it works fine!

Thanx!
 
P

Patrice

Missed that. In C# you tell the number of elements, not the index of the
last element.

Patrice
 
Joined
May 10, 2006
Messages
1
Reaction score
0
Thanks.
Good question, answer and nice codes.

I solved my problem with codes that in the question :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top