asp.net 中用FileStream类实现下载文件功能,可不可以不要把保存路径写死,而自行设定路径,像IE下载一样 20
下面是固定保存路径的代码protectedvoidGridView1_SelectedIndexChanged(objectsender,EventArgse){Grid...
下面是固定保存路径的代码
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream("F:" + "\\" + row.Cells[1].Text, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create("ftp://" + this.DropDownList1.SelectedItem.Text + "/" + row.Cells[1].Text);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("tool", "tool00");
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
Response.Write("<script language='javascript'>alert('" + ex.Message + "');</script>");
}
} 展开
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream("F:" + "\\" + row.Cells[1].Text, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create("ftp://" + this.DropDownList1.SelectedItem.Text + "/" + row.Cells[1].Text);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("tool", "tool00");
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
Response.Write("<script language='javascript'>alert('" + ex.Message + "');</script>");
}
} 展开
2个回答
展开全部
直接用Response内置对象把文件以流的形式传到前端,浏览器自动就会识别下载了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询