1

我需要从 App_Data 文件夹打开文件,我发现了这个代码片段,只是想知道这个代码是否经过优化。

I 文件的类型可以是 .docx、doc、.pdf

try
        {
            string path = Server.MapPath("~/App_Data/File.txt");


            //string = Server.MapPath(strRequest); 
            System.IO.FileInfo file = new System.IO.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/....";
                Response.WriteFile(file.FullName);
                Response.End();
            }
            else
            {
                Response.Write("This file does not exist.");
            }
        }


        catch (Exception rt)
        {
           // Response.Write(rt.Message);
        }

或使用此代码段

FileStream MyFileStream;long FileSize;
string path = Server.MapPath("~/App_Data/aspnetmvc-nerdinner_v1.pdf");
MyFileStream = new FileStream(path, FileMode.Open);
FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize + 1];
MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
MyFileStream.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=MyReport.PDF");
Response.BinaryWrite(Buffer);

请推荐我应该使用哪种方法。我想要简单的是用户单击链接以打开文档。此链接通过电子邮件发送给用户。

更新

我实际上想知道使用过的对象是否正确关闭以及此代码是否以正确的方式处理

4

1 回答 1

0

这里我们需要比较的两点是HttpResponse.WriteFile与代码

FileSize = MyFileStream.Length;
byte[] Buffer = new byte[(int)FileSize + 1];
MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
MyFileStream.Close();
Response.BinaryWrite(Buffer);

我会建议HttpResponse.WriteFile因为正在做完全相同的事情,但需要进行更多检查。此外,上面的代码将文件首先放在内存中,这可能是大文件的问题。

这是内部代码HttpResponse.WriteFile

public void WriteFile(string filename, bool readIntoMemory)
{
    if (filename == null)
    {
        throw new ArgumentNullException("filename");
    }
    filename = this.GetNormalizedFilename(filename);
    FileStream f = null;
    try
    {
        f = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
        if (this.UsingHttpWriter)
        {
            long length = f.Length;
            if (length > 0L)
            {
                // this is false if you call the 
                // HttpResponse.WriteFile(string fileName) 
                if (readIntoMemory)
                {
                    byte[] buffer = new byte[(int) length];
                    int count = f.Read(buffer, 0, (int) length);
                    this._httpWriter.WriteBytes(buffer, 0, count);
                }
                else
                {
                    f.Close();
                    f = null;
                    this._httpWriter.WriteFile(filename, 0L, length);
                }
            }
        }
        else
        {
            this.WriteStreamAsText(f, 0L, -1L);
        }
    }
    finally
    {
        if (f != null)
        {
            f.Close();
        }
    }
}

您也可以使用HttpResponse.WriteFile发送文件而不首先在内存中添加完整文件,这非常好,因为您避免了额外的内存填充。

现在,如果我要编写相同的代码,我将通过在有限缓冲区侧的部分中断文件来发送文件。

于 2013-09-24T07:30:40.663 回答