file download sample

本文介绍了一个ASP.NET应用程序中如何使用C#处理图片显示和文件下载功能,包括从数据库检索图片并直接输出到浏览器,以及根据请求提供文件下载服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 DomainObjects;
using System.Drawing;
using Entities;
using System.IO;

public partial class DisplayVenueImage : System.Web.UI.Page
{
    
//joey
    "Page_Load"#region "Page_Load"
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (Session["UserSession"!= null)
        
{
            UserSession us 
= (UserSession)Session["UserSession"];
            
if (!us.SecurityCheck(us, (int)PermissionIdentity.UserPermission.Add_Venue))
            
{
                Response.Redirect(
"UserDetails.aspx"true);
            }

        }

        
else
        
{
            Response.Redirect(
"BobsleighLogin.aspx"true);
        }

        
try
        
{
            System.Web.HttpContext.Current.Response.ContentType 
= "image/jpeg";
            
int venueID = Convert.ToInt32(this.Request.QueryString["VenueID"]);
            
byte[] bytes = ((VenueImage)(new VenueImageDO()).retrieveVenueImage(venueID)[0]).Image;
            System.Drawing.Image image 
= System.Drawing.Image.FromStream(new System.IO.MemoryStream(bytes));
            image.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        
catch
        
{
            FileStream fs 
= new FileStream(Server.MapPath("Images/noImage.gif"), FileMode.Open, FileAccess.Read);
            
byte[] errorImage = new byte[fs.Length];
            fs.Read(errorImage, 
0, Convert.ToInt32(fs.Length));
            Response.Clear();
            Response.OutputStream.Write(errorImage, 
0, errorImage.Length);
            fs.Close();
        }

        
finally
        
{
            Response.End();
        }

    }

    
#endregion

    
//end
}

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClient;
using System.IO;
using Entities;
using DAO;
using DomainObjects;

public partial class FileDownload : System.Web.UI.Page
{
    
//joey
    "Page_Load"#region "Page_Load"
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (Session["UserSession"!= null)
        
{
            UserSession us 
= (UserSession)Session["UserSession"];
            
if (!us.SecurityCheck(us, (int)PermissionIdentity.UserPermission.Add_Results))
            
{
                Response.Redirect(
"UserDetails.aspx"true);
            }

        }

        
else
        
{
            Response.Redirect(
"BobsleighLogin.aspx"true);
        }

        
try
        
{
            
int VideoID = Convert.ToInt32(this.Request.QueryString["VideoID"]);
            SqlConnection conn 
= new SqlConnection();
            conn.ConnectionString 
= ConfigurationManager.ConnectionStrings["ConnectionInfo"].ConnectionString;
            conn.Open();
            SqlCommand comm 
= new SqlCommand();
            comm.Connection 
= conn;
            comm.CommandText 
= "select * from resultvideo where videoid=@VideoID";
            comm.Parameters.AddWithValue(
"@VideoID", VideoID);
            SqlDataAdapter da 
= new SqlDataAdapter();
            da.SelectCommand 
= comm;
            DataTable dt 
= new DataTable();
            da.Fill(dt);
            
string path = Server.MapPath("~"+ "\\" + ConfigurationManager.AppSettings["VideoFolder"+ dt.Rows[0]["VideoName"].ToString() + dt.Rows[0]["VideoID"].ToString() + dt.Rows[0]["Extension"].ToString();
            FileStream fs 
= new FileStream(path, FileMode.Open, FileAccess.Read);
            
byte[] file = new byte[fs.Length];
            fs.Read(file, 
0, Convert.ToInt32(fs.Length));
            Response.ClearHeaders();
            Response.Clear();
            Response.OutputStream.Write(file, 
0, file.Length);
            fs.Close();
            
string disheader = "attachment; filename=\"" + dt.Rows[0]["VideoName"].ToString() + dt.Rows[0]["VideoID"].ToString() + dt.Rows[0]["Extension"].ToString() + "\"";
            System.Web.HttpContext.Current.Response.AppendHeader(
"Content-Disposition", disheader);
            conn.Close();
        }

        
catch
        
{
        }

        
finally
        
{
            Response.End();
        }

    }

    
#endregion

    
//end
}

转载于:https://www.cnblogs.com/aspxphpjsprb/archive/2008/03/27/1126144.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值