470,831 Members | 1,689 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,831 developers. It's quick & easy.

HtmlInputFile corrupts Excel 2003 (C#)

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

Nov 19 '05 #1
3 3995
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" <ri******@capgemini.com> a écrit dans le message de
news:42**********************@newsreader4.eweka.nl ...
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

Nov 19 '05 #2
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!
Nov 19 '05 #3
Missed that. In C# you tell the number of elements, not the index of the
last element.

Patrice

--

"Rik Moed" <ri******@capgemini.com> a écrit dans le message de
news:42**********************@newsreader4.eweka.nl ...
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!

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Newbie | last post: by
reply views Thread by Pedro Lucas via .NET 247 | last post: by
2 posts views Thread by Skwish | last post: by
2 posts views Thread by Augusto Cesar via DotNetMonster.com | last post: by
3 posts views Thread by UJ | last post: by
7 posts views Thread by Buddy Ackerman | last post: by
3 posts views Thread by renor321 | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.