求文件上传下载c#代码(流的方式+有防盗连的)

babu555 2009-11-06 09:09:48
求文件上传下载c#代码(流的方式+有防盗连的)
...全文
116 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
limii 2009-11-06
  • 打赏
  • 举报
回复
up
Terran5 2009-11-06
  • 打赏
  • 举报
回复
protected void Button1_Click(object sender, EventArgs e)
{
try
{
判断文件大小#region 判断文件大小
int intFileLength = this.FileUpload1.PostedFile.ContentLength;

if (intFileLength > 50000)
{
this.Label1.Text = "文件大于50k,不能上传";
return;
}
#endregion

判断保存的文件夹是否存在#region 判断保存的文件夹是否存在
string strUpPath = @"upfile\" + System.DateTime.Now.ToShortDateString();// +@"\";

//文件夹不存在的时候,创建文件夹
if (!System.IO.Directory.Exists(Server.MapPath(strUpPath)))
{
System.IO.Directory.CreateDirectory(Server.MapPath(strUpPath));
}

string strUrl = Server.MapPath(strUpPath + @"\" + this.FileUpload1.FileName);
#endregion

//上传文件
this.FileUpload1.SaveAs(strUrl);

this.Label1.Text = "文件上传成功";

}
catch (System.Exception ex)
{
this.Label1.Text = "文件上传失败:" + ex.Message;
}
}
zxforben111 2009-11-06
  • 打赏
  • 举报
回复
上传下载做过,但是防盗链没做好。坐看高手!
帮顶。
闪亮的人生 2009-11-06
  • 打赏
  • 举报
回复
给个防盗链的代码
ASP.NET中添加一个img.ashx文件

using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace GetImage
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Img : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpg";
if (context.Request.UrlReferrer != null && context.Request.UrlReferrer.Host.Equals(context.Request.Url.Host, StringComparison.InvariantCultureIgnoreCase))
context.Response.WriteFile(context.Server.MapPath("~/" + context.Request.QueryString["img"]));
else
context.Response.WriteFile(context.Server.MapPath("~/logo.gif"));
}
public bool IsReusable
{
get
{
return false;
}
}
}
}

表示如果来源不为空,并且来源的服务器和当前服务器一致,那就表示是正常访问,非盗链。正常访问文件内容。
否则就是盗链,返回网站LOGO。
jsoner 2009-11-06
  • 打赏
  • 举报
回复
www.itwis.com/html/net/aspnet/20071225/760.html
jsoner 2009-11-06
  • 打赏
  • 举报
回复
防盗连接,你是页面权限要写好啊.跟上传下载没关系的.
上传下载的例子网上很多.
ziminyang 2009-11-06
  • 打赏
  • 举报
回复
不明确说明什么防盗链方式,没法提供具体下载方法

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧