-3

现在我正在处理一个项目,我需要制作一个下载按钮。一切正常,但是当我想下载文件时,我只能单击保存或另存为,当我单击打开时,没有任何反应,为什么?

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();
                }

我忘记了什么?

4

1 回答 1

0

我将删除该行Response.AddHeader("Content-Length", file.Length.ToString());

请关注rfc2616

Content-Length entity-header 字段指示发送给接收者的实体主体的大小,以十进制的八进制数表示,或者在 HEAD 方法的情况下,将发送的实体主体的大小具有请求是 GET。

希望这有帮助。

编辑

顺便说一句,您应该确保filePath在这里使用的变量已经应用Server.MapPath并且不file.Name应该包含空格。

编辑2

根据您的评论,实际上Response.WriteFile这不是一个好的解决方案,它运行缓慢并且大文件有很多问题,您可以在此处参考。您应该按照以下链接通过逐个流读取文件来应用响应流并将其放入内存并发送回客户端,如下所示:

如何将 WebResponse 放入内存流?

或者

http://forums.asp.net/t/1794834.aspx/1

编辑 3:

您在此处发布的代码仍未正确发布 Word 女士,它仍保留您的文档,这就是您的评论出现异常的原因。

请按照以下链接解决您的问题。

处置 Microsoft.Office.Interop.Word.Application

或者

http://code.msdn.microsoft.com/office/CSAutomateWord-f422cae5

于 2013-05-03T16:48:49.657 回答