下载权限控制

jakecheng 2011-05-24 05:12:06
只是我写的一段程序,但是其中有一个部分有错误,我一直弄不懂,哪里错了?应该怎么写?
context.Response.AddHeader("Content-Disposition", string Format("attachment: filename=\"{0}\"",encodename));
这句在编译的时候,出现了
1、错误 1 无效的表达式项“string” E:\CSharp实验\asp\下载及权限控制\ashx\download.ashx.cs 27
2、错误 2 无效的表达式项“)” E:\CSharp实验\asp\下载及权限控制\ashx\download.ashx.cs 27
3、错误 3 应输入 ; E:\CSharp实验\asp\下载及权限控制\ashx\download.ashx.cs 27
4错误 4 应输入 ; E:\c#shiyan\asp1\Default.aspx.cs 25




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.Services;
using 下载及权限控制.dat.UserDateTableAdapters;

namespace 下载及权限控制.ashx
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class download : IHttpHandler,IRequiresSessionState
{

public void ProcessRequest(HttpContext context)
{
//登陆成功时候
if(context.Session["name"]!=null)
{
string filename = context.Request["FileName"];
context.Response.ContentType = "image/JPEG";
string encodename = HttpUtility.UrlEncode(filename);
context.Response.AddHeader("Content-Disposition", string Format("attachment: filename=\"{0}\"",encodename));

int userid=(int)context.Session["userID"];//获取传过来的等级
userinformationTableAdapter getID = new userinformationTableAdapter();
var ID = getID.GetDataByuserID(userid);
var date = ID.Single();


if (date.userID == 1)//会员
{
//获取文件位置并转换成url地址

context.Response.WriteFile("images/"+filename);

}
else//一般用户
{
}

}
//登录失败
else
{
context.Response.Redirect("~/page/登陆界面.aspx");
}
}

public bool IsReusable
{
get
{
return false;
}
}
}
}



请高手指导一下?是不是语句问题?还是?
...全文
86 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
子夜__ 2011-05-24
  • 打赏
  • 举报
回复
#region 文件下载
public bool DownLoadFile(string localPath, string hostURL, int byteCount, string userID, long cruuent)
{

bool result = true;


string tmpURL = hostURL;

byteCount = byteCount * 1024;
hostURL = tmpURL + "&npos=" + cruuent.ToString();

System.IO.FileStream fs;
fs = new FileStream(localPath, FileMode.OpenOrCreate);
if (cruuent > 0)
{
//偏移指针
fs.Seek(cruuent, System.IO.SeekOrigin.Current);
}


System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(hostURL);
if (cruuent > 0)
{
request.AddRange(Convert.ToInt32(cruuent)); //设置Range值
}

try
{
//向服务器请求,获得服务器回应数据流
System.IO.Stream ns = request.GetResponse().GetResponseStream();

byte[] nbytes = new byte[byteCount];
int nReadSize = 0;
nReadSize = ns.Read(nbytes, 0, byteCount);

while (nReadSize > 0)
{
fs.Write(nbytes, 0, nReadSize);
nReadSize = ns.Read(nbytes, 0, byteCount);

}
fs.Close();
ns.Close();
}
catch(Exception ex)
{
LOG.Error("下载" + localPath + "的时候失败!" + "原因是:" + ex.Message);
fs.Close();
result = false;
}
return result;
}
#endregion

ZhuZhuYuXiang 2011-05-24
  • 打赏
  • 举报
回复

把代码行号粘过来,27行有错,是不是少了或者多了个“)”,或者没有结束符“;”好好的检查下
随心录123 2011-05-24
  • 打赏
  • 举报
回复
string strFilePath = Server.MapPath("../Files//" + ds.Tables["files"].Rows[0][0].ToString());
//下载指定的文件
if (File.Exists(strFilePath))
{
System.IO.FileInfo file = new System.IO.FileInfo(strFilePath);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(file.Name));
Response.AppendHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.Flush();
Response.End();
}
ZhuZhuYuXiang 2011-05-24
  • 打赏
  • 举报
回复
using 下载及权限控制.dat.UserDateTableAdapters;

namespace 下载及权限控制.ashx

这个给力!不过用汉语容易出错!


public void FileDownLoad(string fullFileName)
{
FileInfo downFiel = new FileInfo(fullFileName);

string filepath = Server.MapPath("DownFile"+"//"+fullFileName);

FileStream fs = new FileStream(filepath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fullFileName, System.Text.Encoding.UTF8));

Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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