zoukankan      html  css  js  c++  java
  • 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了。

  • 相关阅读:
    关于智能本质的思考
    Effective C++ 条款39
    【视频教程】JEECG 入门视频教程大全+历史版本号代码下载
    HDU 4859(Bestcoder #1 1003)海岸线(网络流之最小割)
    最简单的基于FFMPEG的音频编码器(PCM编码为AAC)
    android经常使用的电话操作
    Vmware中为Mac Os安装vmtools
    win10.10 激活
    VM11安装Mac OS X 10.10
    win7系统升家庭版级为旗舰版的方法
  • 原文地址:https://www.cnblogs.com/wanghk/p/2515437.html
Copyright © 2011-2022 走看看