从FTP下载文件,用户如何提示保存在ASP.NET C#/打开文件 [英] Download file from ftp and how Prompt user to save/open file in ASP.NET C#

查看:133
本文介绍了从FTP下载文件,用户如何提示保存在ASP.NET C#/打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从FTP 下载文件,用户保存在ASP.NET C#

/打开文件怎么提示

 字符串strDownloadURL = System.Configuration.ConfigurationSettings.AppSettings [DownloadURL];
        字符串主机名= System.Configuration.ConfigurationSettings.AppSettings [主机名];
        字符串strUser的= System.Configuration.ConfigurationSettings.AppSettings [BasicAuthenticationUser];
        字符串strPWD = System.Configuration.ConfigurationSettings.AppSettings [BasicAuthenticationPWD];        的FtpWebRequest请求=(的FtpWebRequest)WebRequest.Create(主机名+ strFile);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials =新的NetworkCredential(strUser的,strPWD);
        request.UsePassive = TRUE;
        request.UseBinary = TRUE;
        request.KeepAlive = FALSE;        FtpWebResponse响应=(FtpWebResponse)request.GetResponse();
        流responseStream = response.GetResponseStream();
        字符串文件名= @C:\\ TEMP \\+ strFile +;
        Directory.CreateDirectory(Path.GetDirectoryName(文件名));
        的FileStream文件= File.Create(文件名);
        字节[]缓冲区=新的字节[2 * 1024];
        INT读;
        而((读取= responseStream.Read(缓冲液,0,buffer.Length))大于0){file.Write(缓冲液,0,读); }
        file.Close();
        responseStream.Close();
        response.Close();


解决方案

因此​​,假设你正在写的FTP请求的响应流下来到ASP.NET响应流,并要触发下载对话框中的浏览器, 马上要设置在响应中的内容处置头。

  //注意:因为你是直接写入到客户端,我在你原来的code删除`file`流,因为我们并不需要存储的文件在本地。 ..或因此我假设
    Response.AddHeader(内容处置,附件;文件名=+ strFile);    字节[]缓冲区=新的字节[2 * 1024];
    INT读;
    而((读取= responseStream.Read(缓冲液,0,buffer.Length))大于0)
    {
       Response.OutputStream.Write(缓冲,0,读);
    }    responseStream.Close();
    response.Close();

Download file from ftp and how Prompt user to save/open file in ASP.NET C#

        string strDownloadURL = System.Configuration.ConfigurationSettings.AppSettings["DownloadURL"];
        string HostName = System.Configuration.ConfigurationSettings.AppSettings["HostName"];
        string strUser = System.Configuration.ConfigurationSettings.AppSettings["BasicAuthenticationUser"];
        string strPWD = System.Configuration.ConfigurationSettings.AppSettings["BasicAuthenticationPWD"];

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(HostName + strFile);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential(strUser, strPWD);
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        string fileName = @"c:\temp\" + strFile + "";
        Directory.CreateDirectory(Path.GetDirectoryName(fileName));
        FileStream file = File.Create(fileName);
        byte[] buffer = new byte[2 * 1024];
        int read;
        while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0) { file.Write(buffer, 0, read); }
        file.Close();
        responseStream.Close();
        response.Close();

解决方案

So, assuming you're writing the FTP request's response stream down to the ASP.NET response stream, and want to trigger the download dialog in the browser, you'll want to set the Content-Disposition header in the response.

    // note: since you are writing directly to client, I removed the `file` stream in your original code since we don't need to store the file locally... or so I am assuming
    Response.AddHeader("content-disposition", "attachment;filename=" + strFile);

    byte[] buffer = new byte[2 * 1024];
    int read;
    while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0) 
    { 
       Response.OutputStream.Write(buffer, 0, read);
    }

    responseStream.Close();
    response.Close();

这篇关于从FTP下载文件,用户如何提示保存在ASP.NET C#/打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆