1、在下载页中,我点击下载按钮,要求打开一个新的页面,在新的页面打开时从文件夹里读取这个文件,然后关闭这个页面,就和没打开过一样,要求使用另存不能下载或打开这个文件,试了N次都没成,基本上我能找到的方法和我能想到的全试过了,还是不行。主要是下载那里我弄不好
2、文件的删除也是一样,在删除数据记录时要同时删除对应的文件,绝对路径什么的试过好多次了,还是不行!看看谁有这个能力帮帮我吧,谢谢大家先
我用的是C#.NET
一.为什么要打开一个新页面呢?直接取文件在服务器的路径,然后写入WriterStream,直接Response给客户端就行了呀?
二.删除数据记录时,用FileInfo.Exists判断文件是否存在,是就删除不是就PASS
按1楼所述!本来网上很多此类文章了
1. DateTime dt = DateTime.Now;
string nowtime = dt.Year.ToString()+"-"+dt.Month.ToString()+"-"+dt.Day.ToString();
string path = Server.MapPath("jiesuan\\huikuan"+ nowtime +".txt");
System.IO.FileInfo file = new System.IO.FileInfo(path);
// clear the current output content from the buffer
Response.Clear();
// add the header that specifies the default filename for the Download/SaveAs dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
// add the header that specifies the file size, so that the browser
// can show the download progress
Response.AddHeader("Content-Length", file.Length.ToString());
// specify that the response is a stream that cannot be read by the
// client and must be downloaded
Response.ContentType = "application/octet-stream";
// send the file stream to the client
Response.WriteFile(file.FullName);
// stop the execution of this page
Response.End();
2.删除
string SerFile = Server.MapPath(@"..\UpLoad\MatchFund.txt");
try
{
if(File.Exists(SerFile))
{
File.Delete(SerFile);
}
UpFile.PostedFile.SaveAs(Server.MapPath(@"..\UpLoad\MatchFund.txt"));
MessageInfo.Text = "上传成功!";
}
catch(Exception ee)
{
MessageInfo.Text = "发生异常,请重新上传!错误信息内容为:" + ee.Message;
}
在浏览器中显示
string strFile="F:\\a.doc";
Response.Clear();
Response.ClearHeaders();
Response.Charset = "GB2312";
Response.ContentEncoding =System.Text.Encoding.UTF8;
Response.ContentType = "application/msword";
Response.WriteFile(strFile);
弹出对话框让用户保存
string strFile="F:\\a.doc";
if(!System.IO.File.Exists(strFile))
{
Response.Write("<script language=javascript>alert(对不起,文件不存在!);</script>");
return;
}
Response.Clear();
Response.ClearHeaders();
Response.Charset = "GB2312";
Response.ContentEncoding =System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream";
FileInfo fi=new FileInfo(strFile);
Response.AddHeader("Content-Disposition","attachment; filename=" + HttpUtility.UrlEncode(fi.Name)) ;
Response.AddHeader("Content-Length",fi.Length.ToString());
byte[] tmpbyte=new byte[1024*8];
FileStream fs=fi.OpenRead();
int count;
while((count=fs.Read(tmpbyte,0,tmpbyte.Length))>0)
{
Response.BinaryWrite(tmpbyte);
Response.Flush();
}
fs.Close();
Response.End();
删除文件,先确定文件的路径
string 文件路径=Server.MapPath("")+"\\文件夹名\\"+文件名;//得到文件路径
if(System.IO.File.Exists(imagepath))//判断是否存在此文件
{
System.IO.File.Delete(imagepath);//删除
}