从C#中的其他驱动器检索文档

时间:2013-02-04 11:22:36

标签: c#

我的C#Web应用程序驻留在C驱动器上。 但是,应用程序会从用户接收上载的文档并将其保存在D驱动器上。

如何在D驱动器上的Web应用程序中的HyperLink控件的NavigateUrl属性中指定C驱动器上的文档路径。

1 个答案:

答案 0 :(得分:0)

在服务器端,您可以将文件加载到Stream中,并将此对象作为byte[]

进行响应
public void Page_Load(object sender, System.EventArgs e)
{
    // create a FileStream from a path from local (D:, C:, E:, etc...)
    FileStream file = File.OpenRead(@"d:\folder\yourfile.txt");

    //Convert the stream to an array of bytes.  
    byte[] byteArray = file.ToArray();

    // Clear all content output from the buffer stream
    Response.Clear();

    // Add a HTTP header to the output stream that specifies the default filename
    // for the browser's download dialog
    Response.AddHeader("Content-Disposition", "attachment; filename=foo.txt");

    // Add a HTTP header to the output stream that contains the 
    // content length(File Size). This lets the browser know how much data is being transfered
    Response.AddHeader("Content-Length", byteArray.Length.ToString());

    // Set the HTTP MIME type of the output stream
    Response.ContentType = "application/octet-stream";

    // Write the data out to the client.
    Response.BinaryWrite(byteArray);
}