asp.net C# b/s 系统 怎么实现文件的上传下载。
asp.netC#b/s系统怎么实现文件的上传下载。可以上传文件类型很多如.rar.txt.doc等等。由客户端上传到服务器端。并在服务器端根据时间上传人新建文件夹存放文...
asp.net C# b/s 系统 怎么实现文件的上传下载。
可以上传文件类型很多如.rar .txt .doc等等。 由客户端上传到服务器端。并在服务器端根据时间上传人新建文件夹存放文件
FileUpLoad控件 对大文件的上传支持的好像不怎么好吧。 我现在需要上传大文件。并且支持多文件上传。
。 并且 希望能有进度条。界面友好。。 展开
可以上传文件类型很多如.rar .txt .doc等等。 由客户端上传到服务器端。并在服务器端根据时间上传人新建文件夹存放文件
FileUpLoad控件 对大文件的上传支持的好像不怎么好吧。 我现在需要上传大文件。并且支持多文件上传。
。 并且 希望能有进度条。界面友好。。 展开
7个回答
展开全部
点击上传按钮:
protected void btnupload_Click(object sender, EventArgs e)
{
string file = uploadfile.SaveFile(uploadpic, upleixing,"uploadpic");
if (UpType.ToLower() == "one")
{
Response.Write("<script>parent.document.form1." + htmControl + ".value='" + file + "';</script>");
}
else
{
Response.Write("<script>if(parent.document.form1." + htmControl + ".value==''){parent.document.form1." + htmControl + ".value='" + file + "';}else{parent.document.form1." + htmControl + ".value+='|" + file + "';}</script>");
}
}
类
uploadfile.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
/// <summary>
/// uploadfile 的摘要说明
/// </summary>
public class uploadfile
{
public uploadfile()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 判断文件路径是否存在;
/// 返回创建点日期文件夹路径
/// </summary>
/// <returns></returns>
public static string createFolder()
{
string rtpaht = "";
DateTime datenow = DateTime.Now;
string year = datenow.Year.ToString();
string month = datenow.Month.ToString();
string date = datenow.Day.ToString();
if (Directory.Exists(HttpContext.Current.Server.MapPath("~/uploadpic/" + year + "/" + month + "/" + date + "")) == false)
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/uploadpic/" + year + "/" + month + "/" + date + ""));
}
rtpaht = "" + year + "/" + month + "/" + date + "";
return rtpaht;
}
/// <summary>
/// 保存文件,返回带日期文件夹的路径。
/// </summary>
/// <param name="file"></param>
/// <param name="type"></param>
/// <returns></returns>
public static string SaveFile(FileUpload file,string type)
{
return SaveFile(file, type, "uploadpic");
}
/// <summary>
/// 保存文件
/// </summary>
/// <param name="file"></param>
/// <param name="type"></param>
/// <param name="SaveFoder"></param>
/// <returns></returns>
public static string SaveFile(FileUpload file, string type,string SaveFoder)
{
if (type.IndexOf("asp") >= 0 || type.IndexOf("php") >= 0 || type.IndexOf("aspx") >= 0 || type.IndexOf("jsp") >= 0 || type.IndexOf("exe") >= 0)
{
HttpContext.Current.Response.End();
}
string filename = file.PostedFile.FileName;
if (file.HasFile)
{
string savepath1 = createFolder();
string savepath = "";
if (SaveFoder == "")
{
savepath = HttpContext.Current.Server.MapPath("~/"+SaveFoder+"/" + savepath1);
if (Directory.Exists(savepath) == false)
{
Directory.CreateDirectory(savepath);
}
}
else
{
savepath = HttpContext.Current.Server.MapPath("~/" + SaveFoder + "/" + savepath1);
if (Directory.Exists(savepath) == false)
{
Directory.CreateDirectory(savepath);
}
}
string filename2 = DateTime.Now.ToString().Replace("-", "").Replace(":", "").Replace(" ", "") + "." + GetFileExtends(filename, type);
file.SaveAs(savepath + "/" + filename2);
return savepath1 + "/" + filename2;
}
else
{
// HttpContext.Current.Response.Write(CommdClass.ResponseScript("请选择上传的文件!", "-1"));
return "nofile.jpg";
}
}
/// <summary>
///
/// </summary>
/// <param name="filename"></param>
/// <param name="filetype">文件类型(gif,jpg,bmp)</param>
/// <returns></returns>
public static string GetFileExtends(string filename,string filetype)
{
string ext = null;
if (filename.IndexOf('.') > 0)
{
string[] fs = filename.Split('.');
ext = fs[fs.Length - 1];
}
if (filetype.IndexOf(ext.ToLower()) < 0)
{
HttpContext.Current.Response.Write(ext + "<br>" + filetype);
HttpContext.Current.Response.Write(CommdClass.ResponseScript("文件格式错误,只允许上传" + filetype + "格式文件。", "0"));
return"";
}
return ext;
}
}
protected void btnupload_Click(object sender, EventArgs e)
{
string file = uploadfile.SaveFile(uploadpic, upleixing,"uploadpic");
if (UpType.ToLower() == "one")
{
Response.Write("<script>parent.document.form1." + htmControl + ".value='" + file + "';</script>");
}
else
{
Response.Write("<script>if(parent.document.form1." + htmControl + ".value==''){parent.document.form1." + htmControl + ".value='" + file + "';}else{parent.document.form1." + htmControl + ".value+='|" + file + "';}</script>");
}
}
类
uploadfile.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
/// <summary>
/// uploadfile 的摘要说明
/// </summary>
public class uploadfile
{
public uploadfile()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 判断文件路径是否存在;
/// 返回创建点日期文件夹路径
/// </summary>
/// <returns></returns>
public static string createFolder()
{
string rtpaht = "";
DateTime datenow = DateTime.Now;
string year = datenow.Year.ToString();
string month = datenow.Month.ToString();
string date = datenow.Day.ToString();
if (Directory.Exists(HttpContext.Current.Server.MapPath("~/uploadpic/" + year + "/" + month + "/" + date + "")) == false)
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/uploadpic/" + year + "/" + month + "/" + date + ""));
}
rtpaht = "" + year + "/" + month + "/" + date + "";
return rtpaht;
}
/// <summary>
/// 保存文件,返回带日期文件夹的路径。
/// </summary>
/// <param name="file"></param>
/// <param name="type"></param>
/// <returns></returns>
public static string SaveFile(FileUpload file,string type)
{
return SaveFile(file, type, "uploadpic");
}
/// <summary>
/// 保存文件
/// </summary>
/// <param name="file"></param>
/// <param name="type"></param>
/// <param name="SaveFoder"></param>
/// <returns></returns>
public static string SaveFile(FileUpload file, string type,string SaveFoder)
{
if (type.IndexOf("asp") >= 0 || type.IndexOf("php") >= 0 || type.IndexOf("aspx") >= 0 || type.IndexOf("jsp") >= 0 || type.IndexOf("exe") >= 0)
{
HttpContext.Current.Response.End();
}
string filename = file.PostedFile.FileName;
if (file.HasFile)
{
string savepath1 = createFolder();
string savepath = "";
if (SaveFoder == "")
{
savepath = HttpContext.Current.Server.MapPath("~/"+SaveFoder+"/" + savepath1);
if (Directory.Exists(savepath) == false)
{
Directory.CreateDirectory(savepath);
}
}
else
{
savepath = HttpContext.Current.Server.MapPath("~/" + SaveFoder + "/" + savepath1);
if (Directory.Exists(savepath) == false)
{
Directory.CreateDirectory(savepath);
}
}
string filename2 = DateTime.Now.ToString().Replace("-", "").Replace(":", "").Replace(" ", "") + "." + GetFileExtends(filename, type);
file.SaveAs(savepath + "/" + filename2);
return savepath1 + "/" + filename2;
}
else
{
// HttpContext.Current.Response.Write(CommdClass.ResponseScript("请选择上传的文件!", "-1"));
return "nofile.jpg";
}
}
/// <summary>
///
/// </summary>
/// <param name="filename"></param>
/// <param name="filetype">文件类型(gif,jpg,bmp)</param>
/// <returns></returns>
public static string GetFileExtends(string filename,string filetype)
{
string ext = null;
if (filename.IndexOf('.') > 0)
{
string[] fs = filename.Split('.');
ext = fs[fs.Length - 1];
}
if (filetype.IndexOf(ext.ToLower()) < 0)
{
HttpContext.Current.Response.Write(ext + "<br>" + filetype);
HttpContext.Current.Response.Write(CommdClass.ResponseScript("文件格式错误,只允许上传" + filetype + "格式文件。", "0"));
return"";
}
return ext;
}
}
展开全部
FileUploadImage 上传的ASP 控件名
/// <summary>
/// 上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnGO_Click(object sender, EventArgs e)
{
if (FileUploadImage.HasFile) //文件不能为空 否则进不来
{
//文件类型
string type = FileUploadImage.PostedFile.ContentType;
if (type != "jpg" || type != "bmp" || type != "gif")
{
//文件名
string name = FileUploadImage.FileName;
//服务器路径 ico根目录下文件 如果你的 页面不在根目录下就要看了 根下一级 用 ../ico 二级用 ../../ico 同理推
string path = Server.MapPath("ico");
//上传
FileUploadImage.PostedFile.SaveAs(path + "//" + name);
//图片显示
Image1.ImageUrl = "ico/" + name;
}
}
}
/// <summary>
/// 上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnGO_Click(object sender, EventArgs e)
{
if (FileUploadImage.HasFile) //文件不能为空 否则进不来
{
//文件类型
string type = FileUploadImage.PostedFile.ContentType;
if (type != "jpg" || type != "bmp" || type != "gif")
{
//文件名
string name = FileUploadImage.FileName;
//服务器路径 ico根目录下文件 如果你的 页面不在根目录下就要看了 根下一级 用 ../ico 二级用 ../../ico 同理推
string path = Server.MapPath("ico");
//上传
FileUploadImage.PostedFile.SaveAs(path + "//" + name);
//图片显示
Image1.ImageUrl = "ico/" + name;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
FileUpload 这个控件是上传 FileUpload .saveAs(路径)方法把上传文件传到一个路径中,下载最简单的是直接把url路径连到你那个文件的路径就会直接下载
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这不是一句话能说完的,汗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询