码迷,mamicode.com
首页 > Web开发 > 详细

asp.net从服务器(指定文件夹)下载任意格式的文件到本地

时间:2014-08-07 12:31:09      阅读:437      评论:0      收藏:0      [点我收藏+]

标签:blog   http   os   io   文件   art   ar   html   

一、我需要从服务器下载ppt文件到本地

protected void Btn_DownPPT_Click(object sender, EventArgs e)
        {
            DBService svc = new DBService();
            svc.DownPpts();
            string strFileName = "公报.ppt";
            string filename = Context.Server.MapPath(Context.Request.ApplicationPath) + "\\Temp\\" + strFileName; //物理地址


            if (strFileName != "")
            {
                string path = Context.Server.MapPath(Context.Request.ApplicationPath) + "\\Temp\\" + strFileName;//物理地址
                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/octet-stream";
                    Response.Filter.Close();
                    Response.WriteFile(file.FullName);
                    Response.End();
                }
                else
                {
                    Response.Write("This file does not exist.");
                }
            }
        }

以下转载:http://www.alixixi.com/Dev/Web/ASPNET/aspnet1/2007/2007050633864.html

string filename = "a.txt";

        if (filename != "")
        {

            string path = Server.MapPath(filename);

            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/octet-stream";

                Response.Filter.Close();

                Response.WriteFile(file.FullName);

                Response.End();

            }

            else
            {

                Response.Write("This file does not exist.");

            }

        }

 

转载自:http://blog.csdn.net/zlhzhj/article/details/7563762

  • 局域网文件下载:

public class RemoteDownload
    {
        public static void DownLoad(string addressUrl,string localName)
        {
            //下载文件
            System.Net.WebClient myWebClient = new System.Net.WebClient();
            myWebClient.DownloadFile(@"/10.2.0.254/software/01279.lic.txt", "testdownload.txt");           
            //下载end
        }
    }

 通过URL获取页面内容

   try
            {
                // 远程获取目标页面源码
                string strTargetHtml = string.Empty;
                WebClient wc = new WebClient();
                wc.Credentials = CredentialCache.DefaultCredentials;
                byte[] btPageData = wc.DownloadData(strTargetUrl + dtTargetDate.ToString("yyyy") + "/" + dtTargetDate.ToString("MM") + "/" + dtTargetDate.ToString("dd") + "/");
                strTargetHtml = Encoding.UTF8.GetString(btPageData);
                wc.Dispose();
            }
            catch(Exception exp)
            {
                _isError = true;
                _errorDetail = "获取目标日志文件列表时出错:" + exp.Message;
            }

    

  • 通过web方式,从远程服务器端下载文件:

 

public class WebDownload
    {
        public static void DownLoad(string Url, string FileName)
        {
            bool Value = false;
            WebResponse response = null;
            Stream stream = null;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

                response = request.GetResponse();
                stream = response.GetResponseStream();

                if (!response.ContentType.ToLower().StartsWith("text/"))
                {
                    Value = SaveBinaryFile(response, FileName);

                }

            }
            catch (Exception err)
            {
                string aa = err.ToString();
            }

        }

        /// <summary>
        /// Save a binary file to disk.
        /// </summary>
        /// <param name="response">The response used to save the file</param>
        // 将二进制文件保存到磁盘
        private static bool SaveBinaryFile(WebResponse response, string FileName)
        {
            bool Value = true;
            byte[] buffer = new byte[1024];

            try
            {
                if (File.Exists(FileName))
                    File.Delete(FileName);
                Stream outStream = System.IO.File.Create(FileName);
                Stream inStream = response.GetResponseStream();

                int l;
                do
                {
                    l = inStream.Read(buffer, 0, buffer.Length);
                    if (l > 0)
                        outStream.Write(buffer, 0, l);
                }
                while (l > 0);

                outStream.Close();
                inStream.Close();
            }
            catch
            {
                Value = false;
            }
            return Value;
        }

 

  • 从FTP上下载文件:

 

 public class FtpDownload
    {
        public static void DownLoad(string FtpPath)
        {
            /*首先从配置文件读取ftp的登录信息*/
            string TempFolderPath = System.Configuration.ConfigurationManager.AppSettings["TempFolderPath"].ToString();
            string FtpUserName = System.Configuration.ConfigurationManager.AppSettings["FtpUserName"].ToString();
            string FtpPassWord = System.Configuration.ConfigurationManager.AppSettings["FtpPassWord"].ToString();
            string LocalFileExistsOperation = System.Configuration.ConfigurationManager.AppSettings["LocalFileExistsOperation"].ToString();


            Uri uri = new Uri(FtpPath);
            string FileName = Path.GetFullPath(TempFolderPath) + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(uri.LocalPath);

            //创建一个文件流
            FileStream fs = null;
            Stream responseStream = null;
            try
            {
                //创建一个与FTP服务器联系的FtpWebRequest对象
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
                //设置请求的方法是FTP文件下载
                request.Method = WebRequestMethods.Ftp.DownloadFile;

                //连接登录FTP服务器
                request.Credentials = new NetworkCredential(FtpUserName, FtpPassWord);

                //获取一个请求响应对象
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                //获取请求的响应流
                responseStream = response.GetResponseStream();

                //判断本地文件是否存在,如果存在,则打开和重写本地文件

                if (File.Exists(FileName))
                {
                    if (LocalFileExistsOperation == "write")
                    {
                        fs = File.Open(FileName, FileMode.Open, FileAccess.ReadWrite);

                    }
                }

                //判断本地文件是否存在,如果不存在,则创建本地文件
                else
                {
                    fs = File.Create(FileName);
                }

                if (fs != null)
                {

                    int buffer_count = 65536;
                    byte[] buffer = new byte[buffer_count];
                    int size = 0;
                    while ((size = responseStream.Read(buffer, 0, buffer_count)) > 0)
                    {
                        fs.Write(buffer, 0, size);

                    }
                    fs.Flush();
                    fs.Close();
                    responseStream.Close();
                }
            }
            finally
            {
                if (fs != null)
                    fs.Close();
                if (responseStream != null)
                    responseStream.Close();
            }


        }
    }

asp.net从服务器(指定文件夹)下载任意格式的文件到本地,布布扣,bubuko.com

asp.net从服务器(指定文件夹)下载任意格式的文件到本地

标签:blog   http   os   io   文件   art   ar   html   

原文地址:http://www.cnblogs.com/xuxin-1989/p/3896561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!