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
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