472,805 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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 4068
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Newbie | last post by:
i have a c# asp.net webform that has an HtmlInputFile and a listbox (to hold the filenames to be uploaded). but HtmlInputFile.PostedFile and HtmlInputFile.Value are readonly properties. i have to...
0
by: Pedro Lucas via .NET 247 | last post by:
I have a weird .RC corruption problem with MSVC .NET 2003. I have a .RC file that has been in use for years with .NET inside a project called Netcount. Recently I upgraded to .NET 2003 and...
2
by: Skwish | last post by:
Hi, I have a page with a htmlInputFile that is working fine. However, if I go to another page, and then go back to the page with the htmlInputFile, I get the following error Warning: Page...
5
by: shimonsim | last post by:
Hi, I need to upload pictures from client machine and I am using HtmlInputFile control I set validator to make sure that file has correct expention but if one of the files has incorrect extention...
2
by: Augusto Cesar via DotNetMonster.com | last post by:
Hi, I want to customize the HtmlInputFile. I wanna something like an image button to play the "browse" button hole and hide the textbox. Is that possible? I also have tried to hide an...
3
by: UJ | last post by:
Guys, I know this isn't the appropriate place to post this because it's HTML not ASP.Net but I need some guidance. I have a web page with an HTMLInputFile on it. The person enters the stuff,...
7
by: Buddy Ackerman | last post by:
I created this class Public Class HTMLFileInput : Inherits System.Web.UI.HtmlControls.HtmlInputFile Public Property Data As String Get Return ViewState("HTMLFileInput.Data") End Get Set...
3
by: renor321 | last post by:
Hi, I'm trying to use an HTMLInputFile control to upload files to a web server. When I run this on my local computer (Windows XP, .Net 1.1), it works just fine. When I deploy the app to my dev...
2
by: John Bartley K7AAY | last post by:
When I output a table's values to XLS, one value in a very small table, and only one value, is changed. Here are the values in the table, tblLevel. LEVEL H-14 0 1 1.1
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.