"Object moved to here" error

We currently save the output from the excel object to the memory stream. We then modify the headers on the page and write the stream out to the buffer, flush it, then call response.end. (See code below). We have to use this method (instead of saving directly to excel) to make it around our compression utility.

The problem is, after you close the spreadsheet that is opened, if you try to click on any link that causes an http redirect, you receive an IIS error “Object moved to here”. The ‘here’ word is hyperlinked to the correct page - clicking on it successfully takes you to the requested page.

Any ideas?

Thanks,

Stephen Hewitt

Code:

string filename = HttpContext.Current.Request.PhysicalApplicationPath + “\private\reports\pdffiles\” + GU.GenerateUniqueId(HttpContext.Current.Session) + “.xls”;
MemoryStream oStream;
excel.Save(filename, FileFormatType.Excel97,out oStream);

long FileSize = oStream.Length;

long offset = 0;
long length = 0;
byte[] Buffer = new byte[BLOCKSIZE];
Page.Response.ClearContent();
Page.Response.ClearHeaders();
Page.Response.ContentType=“application/vnd.ms-excel”;
// Page.Response.ContentType=“text/filedown”;
Page.Response.AddHeader(“content-disposition”,“attachment;filename=” + sheet.Name + “.xls”);
Page.Response.AddHeader(“Content-Length”, FileSize.ToString());

while (offset < FileSize) {
//if this is a large spreadsheet we should quit if the user
// cancels the download.
if (!Page.Response.IsClientConnected) break;

length = FileSize - offset;
if (length > BLOCKSIZE) {
length = BLOCKSIZE;
}

oStream.Read(Buffer, 0, (int)length);
Page.Response.BinaryWrite(Buffer);
Page.Response.Flush();
offset += length;
}

Page.Response.Flush();
base.AllowPaging = true;
Page.Response.End();

Hi Stephen,

It’s really a strange problem. I tested your code in my machine and all worked fine. Could you trim you project and send it to me? I will investigate this problem.

We found a way around the problem by saving the file to disk and using JavaScript to open a new window with the file in it.

Thanks,

Steve

Hi

This is a genral IIS problem

The solution that we use is that we redirect the browser to the file , In your exampel :

string myName = GU.GenerateUniqueId(HttpContext.Current.Session) + “.xls”;
string filename = HttpContext.Current.Request.PhysicalApplicationPath + “\private\reports\pdffiles\” + myName;
MemoryStream oStream;
excel.Save(filename, FileFormatType.Excel97);

filename = HttpContext.Current.Request.PhysicalApplicationPath + “\private\reports\pdffiles\” + GU.GenerateUniqueId(HttpContext.Current.Session) + “.xls”;
filename = “http://” + HttpContext.Current.Request.ServerVariables(“SERVER_NAME”) + “/private/reports/pdffiles/” + myName;

HttpContext…Response.Redirect(filename)



However, make sure that your application user have read access to \private\reports\pdffiles<BR>directory