您的位置:首页 > 其它

文件的-------上传,下载,删除.

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: