4

I use this code to export/open files (pdf/xls/doc).

Response.Clear();
Response.Buffer = true;

Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Charset = "UTF-8";
Response.ContentType = mimeType;

Response.AppendHeader("content-disposition", "inline; filename=" + fileName + "." + extension);

Response.BinaryWrite(bytes);
Response.End();

When I open a doc file with Word and click export to xls or pdf, nothing happens until I close Word. After I close Word I am already able to open xls and pdf too.

This problem exists when I open xls with Excel.

What is the reason?

yoozer8
  • 7,361
  • 7
  • 58
  • 93
user829081
  • 164
  • 1
  • 17
  • 2
    Can't you set the `FileOpen` mode on the Stream.. can you show how you are declaring the stream..? also look at this posting for an example and try to use something like this `FileAccess.ReadWrite, FileShare.None` http://stackoverflow.com/questions/3889521/response-addheadercontent-disposition-not-opening-file-in-ie6 – MethodMan Mar 29 '13 at 13:01

2 Answers2

2

The reason is because Word, and other Word-like programs lock the open files to avoid double open again. So the locked file can not be opened again from your application.

Ben
  • 51,770
  • 36
  • 127
  • 149
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • 7
    With all due respect, I think this question belongs to SO. – İsmet Alkan Mar 29 '13 at 12:50
  • 3
    The question about if this is a programming question or not has come up on [meta](http://meta.stackexchange.com/q/174144/158605) – Rachel Mar 29 '13 at 13:09
  • @Rachel Ok, did know about meta, I delete the comments, and the line all ready deleted by Ben (nether vote to close or anything). – Aristos Mar 29 '13 at 13:52
  • @Aristos No problem, I don't think I visited the meta site and really started to understand the community behind running the site until I had over 20k rep as well :) – Rachel Mar 29 '13 at 13:53
1

Depending on the word version you use, I would say that the problem comes from the inline content-disposition

In recent versions of Word ( seen in Word 2010 ), it tries to open the document in WEBDAV mode when inline, which might cause issues (locks, missing cookies or credential when trying to export/print the document)

You may look at your network traffic to see if this WEBDAV behavior is involved, and see if content-disposition attachment solves the problem.

Hope this will help

jbl
  • 15,179
  • 3
  • 34
  • 101