• C# 文件上传,下载解决方案


    //第一步,将文件名,路径单独存入一张表,(为防止文件重复,路径后加一个guid命名的文件夹)
    protected
    void btnSave_Click(object sender, EventArgs e) { if (!File1.HasFile) { Alert("请选择文件!"); return; } string filename = File1.FileName; string savedPath = System.Configuration.ConfigurationManager.AppSettings["UploadFilePath"] + Guid.NewGuid().ToString("N"); try { File1.SaveAs(Server.MapPath(savedPath)); } catch { Alert("文件上传失败!"); return; } string guid = EFContext.Current.ISystemService.ISysFileService.Upload(filename, savedPath, int.Parse(EFContext.Current.UserIdentity)); AddFile(guid); }
     DbCommand dbc = SqlDB.GetStoredProcCommand(sqlCommand);
                SqlDB.AddInParameter(dbc, "Name", SqlDbType.NVarChar, entity.Name);
                SqlDB.AddInParameter(dbc, "Url", SqlDbType.NVarChar, entity.Url);
                SqlDB.AddInParameter(dbc, "Created", SqlDbType.DateTime, entity.Created);
                SqlDB.AddInParameter(dbc, "CreatedBy", SqlDbType.Int, entity.CreatedBy);
                SqlDB.AddOutParameter(dbc, "Id", SqlDbType.Char,36);

    返回guid存入存放文件详细信息的表中
    下载文件时根据此guid去读取文件路径和文件名

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Microsoft.Practices.Unity;
    using System.Text;
    using WelderQualification.Models;
    
    namespace WelderQualification.Components
    {
        public class FileDownloadHandler : EFDataHandler
        {
            public override void DataEvent(HttpContext context)
            {
                string guid = context.Request.QueryString["guid"];
                if (string.IsNullOrEmpty(guid))
                    return;
                guid = guid.Trim();
                if (guid.Length != 36)
                    return;
    
                SysFileInfo info=EFContext.Current.ISystemService.ISysFileService.Get(guid);
                if(info==null||info.CreatedBy==0)
                    return;
                
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(context.Server.MapPath(info.Url));
    
                if (fileInfo.Exists)
                {
                    int ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
                    byte[] buffer = new byte[ChunkSize];
    
                    context.Response.Clear();
                    System.IO.FileStream iStream = System.IO.File.OpenRead(fileInfo.FullName);
                    long dataLengthToRead = iStream.Length;//获取下载的文件总大小
                    context.Response.ContentType = "application/octet-stream";
                    context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(info.Name));
                    while (dataLengthToRead > 0 && context.Response.IsClientConnected)
                    {
                        int lengthRead = iStream.Read(buffer, 0, ChunkSize);//读取的大小
                        context.Response.OutputStream.Write(buffer, 0, lengthRead);
                        context.Response.Flush();
                        dataLengthToRead = dataLengthToRead - lengthRead;
                    }
                    iStream.Close();
                    context.Response.Close();
                }
            }
        }
    }

    见一个ashx页面:

    <%@ WebHandler Language="C#" Class="WelderQualification.Components.FileDownloadHandler" %>

    页面绑定

       <a href='/FileDownLoad.ashx?guid=<%# DataBinder.Eval(Container.DataItem, "Url")%>'
                                    target="_blank">


    这样就OK了。

  • 相关阅读:
    C++ 与 C 的预处理能力
    unicore32linuxgcc 预定义宏
    内核中的原子上下文
    ConText
    PREEMPT_ACTIVE
    对象和类
    java的getClass()函数
    堆栈以及对象的引用
    public、protected、default、private作用域
    android 环境变量搭建
  • 原文地址:https://www.cnblogs.com/wanghk/p/2515437.html
Copyright © 2020-2023  润新知