现在我正在处理一个项目,我需要制作一个下载按钮。一切正常,但是当我想下载文件时,我只能单击保存或另存为,当我单击打开时,没有任何反应,为什么?
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();
}
我忘记了什么?