首页上一页 1 下一页尾页 1 条记录 1/1页
学通ASP.NET的24堂课羊皮卷第21章的qjyy04文件上传下载功能(内详!)第一次来不知道找谁?
发表在ASP.NET图书答疑
2012-07-25
是否精华
是
否
版块置顶:
是
否
上传文件失败,下载却可以,求教。。折腾半天,要命了
代码在底下我先说问题出在哪儿就是这段Button1的事件里的
Bitmap bm = new Bitmap(new System.IO.MemoryStream(image1));一直出错,跟踪了,发现在Web服务里跳过执行Web服务中的代码,所以这段执行不成功,但是总是无法让他成功,不知道为什么
Default.aspx页面
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//声明Arrary
ArrayList arylist = new ArrayList();
string serverpath = Server.MapPath("Files/");
DirectoryInfo dir = new DirectoryInfo(serverpath);
FileInfo[] allfile = dir.GetFiles();
//获取文件名并方法arlist中
foreach (FileInfo file in allfile)
{
string name = file.Name;
arylist.Add((string)name);
}
//绑定listBox控件
this.ListBox1.DataSource = arylist;
this.ListBox1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
// string path = "F:\\cASP.NET\\第二十一章(Web Service)\\xl\\webFile\\Files\\";
// string filepath = path + this.FileUpload1.PostedFile.FileName;
//获取文件完整路径
string filepath = this.FileUpload1.PostedFile.FileName.ToString();
//获取文件名称
string filename = filepath.Substring(filepath.LastIndexOf('\\') + 1);
//获取文件名后缀名
string lastname = filename.Substring(filename.LastIndexOf('.'));
switch (lastname)
{
case ".gif":
case ".jpg":
case ".bmp":
//在此处放置用户代码以初始化页面
//定义并初始化文件对象
FileService oImage = new FileService();
//得到二进制文件字节数组;
byte[] image1 = oImage.GetBinaryFile(filepath);
//定义并实例化Bitmap对象 ;
Bitmap bm = new Bitmap(new System.IO.MemoryStream(image1));
//根据不同的条件进行输出或者下载
Response.Clear();
//如果请求字符串指定下载,就下载该图片
//否则,就显示在浏览器中
string serverpath = Server.MapPath("Files/");
if (File.Exists(serverpath + filename))
{
Response.Write("<script>alert('该文件已经存在,请重新命名!');location='Default.aspx'</script>");
}
else
{
bm.Save(serverpath + filename);
Response.Write("<script>alert('图片上传成功!');location='Default.aspx'</script>");
}
break;
default:
Response.Write("<script>alert('不支持该图片格式上传!');location='Default.aspx'</script>");
return;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (this.ListBox1.SelectedValue == "")
{
Response.Write("<script>alert('请选择要下载的图片!');location='Default.aspx'</script>");
}
//获取文件名后缀名
string lastname = this.ListBox1.SelectedValue.Substring(this.ListBox1.SelectedValue.LastIndexOf("."));
//获取所选文件的服务器路径
string serverpath = Server.MapPath("Files/" + this.ListBox1.SelectedValue);
//在此处放置用户代码以初始化页面
//定义并初始化文件对象
FileService oImage = new FileService();
//得到二进制文件字节数组
byte[] image = oImage.GetBinaryFile(serverpath);
//转换为支持存储区的内存流
System.IO.MemoryStream memStream = new System.IO.MemoryStream(image);
//定义并实例化Bitmap对象
// Bitmap bm = new Bitmap(memStream);
//根据不同的条件进行输出或者下载;
Response.Clear();
//如果请求字符串指定下载,就下载该图片
//否则,就显示在浏览器中。
FileInfo file = new FileInfo(serverpath);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(this.ListBox1.SelectedValue));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream; charset=gb2312";
Response.Filter.Close();
Response.WriteFile(file.FullName);
Response.End();
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.RadioButtonList1.SelectedIndex == 0)
{
this.Panel1.Visible = true;
this.Panel2.Visible = false;
}
else {
this.Panel1.Visible = false;
this.Panel2.Visible = true;
}
}
}
WebService里的
[WebMethod]
public byte[] GetBinaryFile(string filename)
{
if (File.Exists(filename))//判断文件是否存在
{
try
{
//打开现有文件以进行读取。
FileStream s = File.OpenRead(filename);
return ConvertFileToByteBuffer(s);
}
catch (Exception e)
{
return new byte[0];
}
}
else
{
return new byte[0];
}
}
/// <summary>
/// 把定义的文件流转换为二进制字节数组
/// </summary>
/// <param name="theStream"></param>
/// <returns></returns>
public byte[] ConvertFileToByteBuffer(System.IO.Stream theStream)
{
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1 = theStream.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray(); //返回二进制字节数组
}
}
代码在底下我先说问题出在哪儿就是这段Button1的事件里的
Bitmap bm = new Bitmap(new System.IO.MemoryStream(image1));一直出错,跟踪了,发现在Web服务里跳过执行Web服务中的代码,所以这段执行不成功,但是总是无法让他成功,不知道为什么
Default.aspx页面
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//声明Arrary
ArrayList arylist = new ArrayList();
string serverpath = Server.MapPath("Files/");
DirectoryInfo dir = new DirectoryInfo(serverpath);
FileInfo[] allfile = dir.GetFiles();
//获取文件名并方法arlist中
foreach (FileInfo file in allfile)
{
string name = file.Name;
arylist.Add((string)name);
}
//绑定listBox控件
this.ListBox1.DataSource = arylist;
this.ListBox1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
// string path = "F:\\cASP.NET\\第二十一章(Web Service)\\xl\\webFile\\Files\\";
// string filepath = path + this.FileUpload1.PostedFile.FileName;
//获取文件完整路径
string filepath = this.FileUpload1.PostedFile.FileName.ToString();
//获取文件名称
string filename = filepath.Substring(filepath.LastIndexOf('\\') + 1);
//获取文件名后缀名
string lastname = filename.Substring(filename.LastIndexOf('.'));
switch (lastname)
{
case ".gif":
case ".jpg":
case ".bmp":
//在此处放置用户代码以初始化页面
//定义并初始化文件对象
FileService oImage = new FileService();
//得到二进制文件字节数组;
byte[] image1 = oImage.GetBinaryFile(filepath);
//定义并实例化Bitmap对象 ;
Bitmap bm = new Bitmap(new System.IO.MemoryStream(image1));
//根据不同的条件进行输出或者下载
Response.Clear();
//如果请求字符串指定下载,就下载该图片
//否则,就显示在浏览器中
string serverpath = Server.MapPath("Files/");
if (File.Exists(serverpath + filename))
{
Response.Write("<script>alert('该文件已经存在,请重新命名!');location='Default.aspx'</script>");
}
else
{
bm.Save(serverpath + filename);
Response.Write("<script>alert('图片上传成功!');location='Default.aspx'</script>");
}
break;
default:
Response.Write("<script>alert('不支持该图片格式上传!');location='Default.aspx'</script>");
return;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (this.ListBox1.SelectedValue == "")
{
Response.Write("<script>alert('请选择要下载的图片!');location='Default.aspx'</script>");
}
//获取文件名后缀名
string lastname = this.ListBox1.SelectedValue.Substring(this.ListBox1.SelectedValue.LastIndexOf("."));
//获取所选文件的服务器路径
string serverpath = Server.MapPath("Files/" + this.ListBox1.SelectedValue);
//在此处放置用户代码以初始化页面
//定义并初始化文件对象
FileService oImage = new FileService();
//得到二进制文件字节数组
byte[] image = oImage.GetBinaryFile(serverpath);
//转换为支持存储区的内存流
System.IO.MemoryStream memStream = new System.IO.MemoryStream(image);
//定义并实例化Bitmap对象
// Bitmap bm = new Bitmap(memStream);
//根据不同的条件进行输出或者下载;
Response.Clear();
//如果请求字符串指定下载,就下载该图片
//否则,就显示在浏览器中。
FileInfo file = new FileInfo(serverpath);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(this.ListBox1.SelectedValue));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream; charset=gb2312";
Response.Filter.Close();
Response.WriteFile(file.FullName);
Response.End();
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.RadioButtonList1.SelectedIndex == 0)
{
this.Panel1.Visible = true;
this.Panel2.Visible = false;
}
else {
this.Panel1.Visible = false;
this.Panel2.Visible = true;
}
}
}
WebService里的
[WebMethod]
public byte[] GetBinaryFile(string filename)
{
if (File.Exists(filename))//判断文件是否存在
{
try
{
//打开现有文件以进行读取。
FileStream s = File.OpenRead(filename);
return ConvertFileToByteBuffer(s);
}
catch (Exception e)
{
return new byte[0];
}
}
else
{
return new byte[0];
}
}
/// <summary>
/// 把定义的文件流转换为二进制字节数组
/// </summary>
/// <param name="theStream"></param>
/// <returns></returns>
public byte[] ConvertFileToByteBuffer(System.IO.Stream theStream)
{
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1 = theStream.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray(); //返回二进制字节数组
}
}