附件上传下载的两种方式

本文探讨了两种附件上传下载的方式:一是通过数据库存储,包括上传至数据库的代码示例和从数据库下载的方法;二是采用文件路径保存,详细介绍了如何将附件保存至指定路径,并直接从路径读取附件内容。

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

 

一、数据库方式:

  • 上传至数据库代码:

       

代码片段
string StencilName = this.UploadFile.PostedFile.FileName.ToString();
StencilName = StencilName.Substring(StencilName.LastIndexOf("//") + 1, StencilName.Length - StencilName.LastIndexOf("//") - 1);
tcModel.FileName = StencilName;
Byte[] StencilEntity = new byte[this.UploadFile.PostedFile.ContentLength];
this.UploadFile.PostedFile.InputStream.Read(StencilEntity, 0, this.UploadFile.PostedFile.ContentLength);
tcModel.FileEntity = StencilEntity;

 

  •  从数据库下载:
    代码片段:
    string SeqNum = this.Request["SeqNum"].ToString();
    string AttName = "";
    string strSelect = "select FileName,FileEntity from TechnicContent where TecId="" + SeqNum + """;
    IDataReader myReader = cSqlHelper.ExecuteReader(strSelect);
    int bufferSize = 30000; // Size of the BLOB buffer.
    byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be filled by GetBytes.
    long retval; // The bytes returned from GetBytes.
    long startIndex = 0; // The starting position in the BLOB output.
    //string pub_id = ""; // The publisher id to use in the file name.
    this.Response.Clear();
    this.Response.ContentType = "application/x-msdownload";
    this.Response.ContentEncoding = Encoding.Default;
    while (myReader.Read())
    {
    // Get the publisher id, which must occur before getting the logo.
    AttName = myReader.GetString(0);
    //this.Response.AddHeader("Content-disposition","attachment;StencilName="+ Encoding.Default.GetString(Encoding.Unicode.GetBytes(StencilName)));
    this.Response.AddHeader("Content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(AttName));
    // Read the bytes into outbyte[] and retain the number of bytes returned.
    retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
    this.Response.BinaryWrite(outbyte);
    // Continue reading and writing while there are bytes beyond the size of the buffer.
    while (retval == bufferSize)
    {

    // Reposition the start index to the end of the last buffe0r and fill the buffer.
    startIndex += bufferSize;
    retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
    this.Response.BinaryWrite(outbyte);
    }
    }
    this.Response.End();
    // Close the reader and the connection.
    myReader.Close();

一、文件路径方式:

  • 保存至路径:

 

代码片段:

        // 灾情图片
        // 先判断上传的图片大小应该小于100k,把图片存到image文件夹的Disaster里面,表里只存图片名称
        string fullName = this.UploadFile.PostedFile.FileName; // 得到文件名称
        string exeName = string.Empty;
        if (fullName != string.Empty)
        {
            if (this.UploadFile.PostedFile.ContentLength > 102400)
            {
                Response.Write(Common.CommonClass.ShowMessageBox("请选择小于100k的照片"));
                return;
            }
            exeName = fullName.Substring(fullName.LastIndexOf(".") + 1);
            if (exeName == "jpg" || exeName == "bmp" || exeName == "gif" || exeName == "JPG" || exeName == "BMP" || exeName == "GIF")
            {
            }
            else
            {
                Response.Write(Common.CommonClass.ShowMessageBox("只能上传jpg,gif,bmp格式的文件!!!"));
                return;
            }
            Guid gid = new Guid();
            string fullimageurl = Request.MapPath("~//images//Disaster").ToString() + "//" + gid.ToString() + "." + exeName;
            HttpPostedFile postedFile = this.UploadFile.PostedFile; //得到要上传文件
            Common.CommonClass.SaveFile(postedFile, fullimageurl);
            model.Pictrue = gid.ToString() + "." + exeName;
        }
  • 读取:直接从默认路径中取就可以了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值