473,513 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4113
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
2115
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
1370
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
1948
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
2886
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
1657
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
4723
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
1747
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
1176
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
1847
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
0
7265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7388
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.