当前位置: 移动技术网 > IT编程>开发语言>.net > 下载文件时-修改文件名字

下载文件时-修改文件名字

2019年04月02日  | 移动技术网IT编程  | 我要评论

茉莉清茶官网,李思福,武动乾坤 快眼看书

1后台代码

  /// <summary>
        /// 文件下载2
        /// </summary>
        /// <param name="filename">需要修改的文件名</param>
        /// <param name="filepath">文件路径路径</param>
        public void bigfiledownload(string filename, string filepath)
        {
            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 = system.web.httpcontext.current.server.mappath(filepath);

            // 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/octet-stream";
                response.addheader("content-disposition", "attachment; filename=" + system.web.httputility.urlencode(filename));//system.text.utf8encoding.utf8.getbytes(filename)

                // 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.
                string message = ex.message;

            }
            finally
            {
                if (istream != null)
                {
                    //close the file.
                    istream.close();
                }
            }
        }

2】前端js代码

  //2下载
                        document.getelementbyid("ifrload").setattribute("src", encodeuri("/admin/ts/ts?filepath=" + url + "&filename=" + name));
                 

3】body中的代码

    @*文件下载使用,勿删除*@
    <iframe id="ifrload" frameborder="0" name="weidu" scrolling="no"></iframe>

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网