用C#怎么实现向网上上传和下载文件

用ASP.next做网站
2025-02-26 10:05:08
推荐回答(4个)
回答1:

上传用FileUpload控件
下面是点击上传事件的代码:
if (this.fudUp.HasFile)//检查是否有文件
{
string fullFileName = this.fudUp.PostedFile.FileName; //文件路径名
string oldFullFileName = Server.MapPath(oldPic); //旧文件路径名
string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1); //图片名称
string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1); //图片格式

if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp") //判断是否为图片类型
{
if (this.fudUp.PostedFile.ContentLength > 100 * 1024)
{
Response.Write("");
}
else
{
string filePath = "~/User/image/" + user.Id;
string name = DateTime.Now.Ticks.ToString();
if (Directory.Exists(Server.MapPath(filePath)) == false)//如果不存在就创建file文件夹
{
Directory.CreateDirectory(Server.MapPath(filePath));
}
string path = HttpContext.Current.Request.MapPath(filePath + "/");//获取上传文件的网站目录路径
this.fudUp.SaveAs(path + name + "." + type);//存储文件到磁盘

user.Other1 = filePath + "/" + name + "." + type;
try
{
Tb_UserManager.ModifyTb_UserOther(user);//修改数据库里的数据
System.IO.File.Delete(oldFullFileName);//删除文件夹里原有的图片
}
catch (Exception)
{
Response.Write("");//提示
throw;
}
Response.Write("");//提示

//this.imgPicture.Src = "~/zp/" + fileName;//显示图片
}

}
else
{
Response.Write("");
}
}
else
{
Response.Write("");
}
下载可以直接用连接文件地址就可以下载

回答2:

上传用FileUpload控件。用户选择上传文件之后提交,用FileUpLoad的.SaveAs(path)方法,保存至服务器硬盘。具体操作请百度搜索。

下载可以直接用下载abcd.rar文件
超链接就可以实现。

回答3:

看一下System.Net命名空间的WebClient类

回答4:

可参考:http://www.pconline.com.cn/pcedu/empolder/net/cs/0504/601144.html
希望对你有帮助