文件的-------上传,下载,删除.
2007-09-21 12:42
274 查看
#region 下载查看文件方法
public bool downAndlook(string fileserverURL)
{
try
{
string fileserverpath = Page.Server.MapPath(fileserverURL);
System.IO.FileInfo fi = new System.IO.FileInfo(fileserverpath);
fi.Attributes = System.IO.FileAttributes.Normal;
System.IO.FileStream filestream = new System.IO.FileStream(fileserverpath, System.IO.FileMode.Open);
long filesize = filestream.Length;
int i = Convert.ToInt32(filesize);
Page.Response.ContentType = "application/octet-stream";
Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileserverpath, System.Text.Encoding.UTF8));
Page.Response.AddHeader("Content-Length", filesize.ToString());
byte[] fileBuffer = new byte[i];
filestream.Read(fileBuffer, 0, i);
filestream.Close();
Page.Response.BinaryWrite(fileBuffer);
Page.Response.End();
return true;
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.GetType().ToString() + "');</script>");
return false;
}
}
#endregion
#region 删除文件方法
public bool DeleteFile(string filename)
{
string filePath = Server.MapPath(".") + "//" + filename;
System.IO.FileInfo fi = new System.IO.FileInfo(filePath);
fi.Attributes = System.IO.FileAttributes.Normal;
if (System.IO.File.Exists(filePath) == false)
{
return false;
}
else
{
System.IO.File.Delete(filePath);
return true;
}
}
#endregion
#region 上传方法
public bool stringbind(string filestringPath)
{
HttpFileCollection files = HttpContext.Current.Request.Files;
/// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
System.Text.StringBuilder strMsg2 = new System.Text.StringBuilder();
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
if (postedFile.ContentLength > 41943040)//判断上传文件的大小,前提是已经在Web.Config中设置了最大上传文件的大小为
{ //< 40M 设置内容这:“<httpRuntime executionTimeout="300" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/>”
Response.Write("<script>alert('文件过大,上传失败!');</script>");
}
else
{
if (postedFile.ContentLength > 0 && (postedFile.FileName.ToString() != ""))
{
string fileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf("//"));
// 判断上传的文件是否存在.....
if (System.IO.File.Exists(Page.Server.MapPath(".") + "//" + filestringPath + fileName) == true)
{
string filesName = System.IO.Path.GetFileName(postedFile.FileName);
//如果上传的文件存在,则不上传,并且显示上传失败的文件名.
string lbl2 = strMsg2.Append(filesName + ";").ToString();
Response.Write("<script>alert('你上载的文件中'" + lbl2 + "'已经存在,请更改此文件名称后,重新上传!');</script>");
}
else
{
if (System.IO.File.Exists(Page.Server.MapPath(".") + "//" + filestringPath + fileName) == false)
{
string filesName = System.IO.Path.GetFileName(postedFile.FileName);
System.IO.Directory.CreateDirectory(Page.Server.MapPath(".") + "//" + filestringPath);
postedFile.SaveAs(Page.Server.MapPath(".") + "//" + filestringPath + fileName);
string FIleServerPath = Page.Server.MapPath(".") + "//" + filestringPath + fileName;//上传到服务器中的/路径
string FileNoServerPath = filestringPath + fileName;
string lbl = strMsg.Append(filesName + ";").ToString();
#region 插入企业闲置商标附件
NetEFO.FunctionLib f = new NetEFO.FunctionLib();
oXZSBFiles_Fid = " ";
int FBase_XZSBFilesTypeID = 1;
string sqlFileString = "insert Bill_XZSBFiles values(" + oXZSBFiles_Fid + "," + Fid + "," + FBase_XZSBFilesTypeID +
",'" + filesName + "','" + FIleServerPath + "','" + FileNoServerPath + "')";
DBO.Insert(CnnString, sqlFileString);
#endregion
}
}
}
}
}
return true;
}
catch
{
return false;
}
}
#endregion
另外在Web.Config中加入 <httpRuntime executionTimeout="300" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/> 能够设置上传文件的大小为 40M.
public bool downAndlook(string fileserverURL)
{
try
{
string fileserverpath = Page.Server.MapPath(fileserverURL);
System.IO.FileInfo fi = new System.IO.FileInfo(fileserverpath);
fi.Attributes = System.IO.FileAttributes.Normal;
System.IO.FileStream filestream = new System.IO.FileStream(fileserverpath, System.IO.FileMode.Open);
long filesize = filestream.Length;
int i = Convert.ToInt32(filesize);
Page.Response.ContentType = "application/octet-stream";
Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileserverpath, System.Text.Encoding.UTF8));
Page.Response.AddHeader("Content-Length", filesize.ToString());
byte[] fileBuffer = new byte[i];
filestream.Read(fileBuffer, 0, i);
filestream.Close();
Page.Response.BinaryWrite(fileBuffer);
Page.Response.End();
return true;
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.GetType().ToString() + "');</script>");
return false;
}
}
#endregion
#region 删除文件方法
public bool DeleteFile(string filename)
{
string filePath = Server.MapPath(".") + "//" + filename;
System.IO.FileInfo fi = new System.IO.FileInfo(filePath);
fi.Attributes = System.IO.FileAttributes.Normal;
if (System.IO.File.Exists(filePath) == false)
{
return false;
}
else
{
System.IO.File.Delete(filePath);
return true;
}
}
#endregion
#region 上传方法
public bool stringbind(string filestringPath)
{
HttpFileCollection files = HttpContext.Current.Request.Files;
/// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
System.Text.StringBuilder strMsg2 = new System.Text.StringBuilder();
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
if (postedFile.ContentLength > 41943040)//判断上传文件的大小,前提是已经在Web.Config中设置了最大上传文件的大小为
{ //< 40M 设置内容这:“<httpRuntime executionTimeout="300" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/>”
Response.Write("<script>alert('文件过大,上传失败!');</script>");
}
else
{
if (postedFile.ContentLength > 0 && (postedFile.FileName.ToString() != ""))
{
string fileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf("//"));
// 判断上传的文件是否存在.....
if (System.IO.File.Exists(Page.Server.MapPath(".") + "//" + filestringPath + fileName) == true)
{
string filesName = System.IO.Path.GetFileName(postedFile.FileName);
//如果上传的文件存在,则不上传,并且显示上传失败的文件名.
string lbl2 = strMsg2.Append(filesName + ";").ToString();
Response.Write("<script>alert('你上载的文件中'" + lbl2 + "'已经存在,请更改此文件名称后,重新上传!');</script>");
}
else
{
if (System.IO.File.Exists(Page.Server.MapPath(".") + "//" + filestringPath + fileName) == false)
{
string filesName = System.IO.Path.GetFileName(postedFile.FileName);
System.IO.Directory.CreateDirectory(Page.Server.MapPath(".") + "//" + filestringPath);
postedFile.SaveAs(Page.Server.MapPath(".") + "//" + filestringPath + fileName);
string FIleServerPath = Page.Server.MapPath(".") + "//" + filestringPath + fileName;//上传到服务器中的/路径
string FileNoServerPath = filestringPath + fileName;
string lbl = strMsg.Append(filesName + ";").ToString();
#region 插入企业闲置商标附件
NetEFO.FunctionLib f = new NetEFO.FunctionLib();
oXZSBFiles_Fid = " ";
int FBase_XZSBFilesTypeID = 1;
string sqlFileString = "insert Bill_XZSBFiles values(" + oXZSBFiles_Fid + "," + Fid + "," + FBase_XZSBFilesTypeID +
",'" + filesName + "','" + FIleServerPath + "','" + FileNoServerPath + "')";
DBO.Insert(CnnString, sqlFileString);
#endregion
}
}
}
}
}
return true;
}
catch
{
return false;
}
}
#endregion
另外在Web.Config中加入 <httpRuntime executionTimeout="300" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/> 能够设置上传文件的大小为 40M.
相关文章推荐
- 使用java操作FastDFS上传,下载,删除文件
- [导入].net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- 文件上传下载所需工具类(文件上传、下载、打包下载、删除)
- java FTP 上传下载删除文件
- .Net 2.0对文件传输协议(FTP)操作(上传,下载,新建,删除,FTP间传送文件等)
- java基于Apache FTP实现文件上传、下载、修改文件名、删除
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等(转)
- java实现文件的上传、下载和删除的后台代码
- 文件上传,下载,删除
- 多条件分页,增加,修改,删除(包含文件上传,下载)(图书管理系统)
- Java利用apache ftp工具实现文件上传下载和删除功能
- thinkphp对文件的上传,删除,下载操作
- SpringMVC ajax技术无刷新文件上传下载删除示例
- [转]文件传输协议(FTP)操作(上传,下载,新建,删除,FTP间传送文件等)实现汇总1
- git:上传、下载、添加、删除文件到远程仓库
- C#对FTP的操作(上传,下载,重命名文件,删除文件,文件存在检查)
- SFTP连接、上传、下载、删除文件
- 利用Apache的commons-net实现FTP的文件读取、上传、下载、删除和移动功能
- silverlight webclient实现上传、下载、删除、读取文件
- C# 文件操作(上传 下载 删除 文件列表...)