現在、ダウンロードボタンを作成する必要があるプロジェクトに取り組んでいます。すべて正常に動作しますが、ファイルをダウンロードするときにしか [保存] または [名前を付けて保存] をクリックできません。[開く] をクリックしても何も起こりません。なぜですか?
string path = filePath.ToString();
FileInfo file = new FileInfo(path);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/msword"; //octet-stream
Response.WriteFile(file.FullName);
Response.End();
}
更新 *保存のみを開くには機能しません
private void SetWordDocument()
{
string strFileName = CleanUp(LabelFirstName.Text + "_" +
LabelLastName.Text + "_" +
DateTime.Now.ToString("yyyy-MM-dd") + "." +
DropDownListDownloadCv0.SelectedItem.Text);
object fs = Server.MapPath("~/Upload/") + strFileName;
using (var db = new knowitCVdbEntities())
{
var theEmpl = (from p in db.EMPLOYEES
where p.username == strUserName
select p).FirstOrDefault();
if (theEmpl != null)
{
object missing = Missing.Value;
object start1 = 0;
var wordApp = new ApplicationClass();
var myDoc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
Range rng = myDoc.Range(ref start1, ref missing);
try
{
const char newLine = (char)11;
myDoc.SaveAs(ref fs,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
}
finally
{
myDoc.Save();
//myDoc.Close(ref doNotSaveChanges, ref missing, ref missing);
wordApp.Quit(ref doNotSaveChanges,ref missing,ref missing);
myDoc = null;
wordApp = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(myDoc);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
GC.Collect();
System.IO.Stream iStream = null;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];
// Length of the file:
int length;
// Total bytes to read:
long dataToRead;
// Identify the file to download including its path.
string filepath = fs.ToString();
// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);
try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read);
// Total bytes to read:
dataToRead = iStream.Length;
Response.ContentType = "application/msword";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
//application/octet-stream
// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
Response.Close();
}
私は何を忘れましたか?