在ASP.NET中动态修改文件下载[翻译][3]

[入库:2005年8月18日] [更新:2007年3月27日]

本文简介:选择自 luyiping 的 blog

    }                                                                                                                           
    finally {                                                                                                                  
        stream.close();                                                                                                   
    }                                                                                                                           
}
    根据rfc 2616 和 rfc 1806 我们需要指出content-type 和 content-disposition 文件头字段是通过下列信息来传输二进制数据的。
response.contenttype = "application/octet-stream"
response.appendheader("content-disposition", "attachment;filename=" + "testdownload.exe");

    在将数据写进http响应流之前,请先写这段代码。

修改文件

    决定二进制数据需要修改的位置有些困难。如果你有一个普通的可执行应用文件在固定的位置能够包含可执行的资源或是在随机的位置包含代码。这大多数取决于你所要完成的任务并能根据不同的可下载文件作出改变。其他解决方案是用参数初始化批文件并使用定制的参数来重新编译你的应用程序或包。
    假设我们发现文件内正确的位置并且需要用用户输入的新数据替换原始内容:

private void patchdata(byte[] buf, string username, int position) {
    byte[] patch = encoding.unicode.getbytes(username);
    system.array.copy(patch, 0, buf, position, patch.length);
}
    我们同时假设文件不是很大,能够被加载到单个内存缓冲区中。
    因为可下载的可执行文件也许会经常被重新编译和替换,填充的位置也经常改变。所以不要将这些参数在asp.net dll代码中进行硬编码而是将它们放入如web.config文件中将是非常明智的。

private void lnkdownload_click(object sender, system.eventargs e) {
    string filename = configurationsettings.appsettings["filename"];
    int position = convert.toint32(configurationsettings.appsettings["position"]);

    filestream stream = new filestream(server.mappath(filename), filemode.open,
    fileaccess.read, fileshare.read);
    try {
        response.contenttype = "application/octet-stream"

本文关键:,在ASP.NET中动态修改文件下载[翻译],
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top