C# web 上传下载文件文件类型不限制,只要客户单有相对应的文件

//上传

//上下文

System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
string[] rd = Request.Form[1].Split(',');//获得图片描述的文本框字符串数组,为对应的图片的描述

//string albumid=ddlAlbum.SelectedValue.Trim();

int ifile;
for (ifile = 0; ifile < files.Count; ifile++)
{
if (files[ifile].FileName.Length > 0)
{
System.Web.HttpPostedFile postedfile = files[ifile];
if (postedfile.ContentLength / 1024 > 1024*30)//单个文件不能大于30M
{
strmsg.Append(Path.GetFileName(postedfile.FileName) + "---不能大于30M<br>");
ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('"+strmsg+"');</script>");
return;
}
string fex = Path.GetExtension(postedfile.FileName);
}
}
if (strmsg.Length <= 0)//说明图片大小和格式都没问题
{
//以下为创建图库目录
string dirpath = Server.MapPath("~/download/sop"); //项目文件的相对路径
string ppath = "";

if (Directory.Exists(dirpath) == false)
{
Directory.CreateDirectory(dirpath);
}

for (int i = 0; i < files.Count; i++)
{
System.Web.HttpPostedFile myFile = files[i];
string FileName = "";
string FileExtention = "";
FileName = System.IO.Path.GetFileName(myFile.FileName);

if (FileName.Length > 0)//有文件才执行上传操作再保存到数据库
{
FileExtention = System.IO.Path.GetExtension(myFile.FileName);

ppath = dirpath + @"\" + FileName;
//重复提示!
bool flag = File.Exists(ppath);
if (flag == true)
{
Response.Write("<script>alert('"+ppath+" 已经存在,请检查!')</script>");
return;
}
myFile.SaveAs(ppath);

txtAttch.Value = FileName;

}

}

System.Threading.Thread.Sleep(150);

if (ppath.Length < 1)
{
Response.Write("<script>alert('请找到符合的文件!')</script>");
return;
}

Response.Write("<script>alert('文件上传成功!')</script>");
}

//下载

string strResult = string.Empty;
string strPath = Server.MapPath(@"~/download/sop"); //项目的相对路径
LinkButton txtlink = (LinkButton)sender;                       //传入linkbutton中的值 (gridview 中的)
string strFile = strPath+"\\"+ txtlink.Text;
using (FileStream fs = new FileStream(strFile, FileMode.Open))  //文件流
{
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(strFile, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();

转载于:https://www.cnblogs.com/hzf08/p/9020760.html

C# web 上传下载文件相关推荐

  1. python实现文件下载-python实现上传下载文件功能

    最近刚学python,遇到上传下载文件功能需求,记录下! django web项目,前端上传控件用的是uploadify. 文件上传 - 后台view 的 Python代码如下: @csrf_exem ...

  2. python实现文件上传功能_python实现上传下载文件功能

    最近刚学python,遇到上传下载文件功能需求,记录下! django web项目,前端上传控件用的是uploadify. 文件上传 - 后台view 的 Python代码如下: @csrf_exem ...

  3. java spring mvc 上传_Java Spring MVC 上传下载文件配置及controller方法详解

    下载: 1.在spring-mvc中配置(用于100M以下的文件下载) 下载文件代码 @RequestMapping("/file/{name.rp}") public Respo ...

  4. python 文件上传下载,python实现上传下载文件功能

    最近刚学python,遇到上传下载文件功能需求,记录下! django web项目,前端上传控件用的是uploadify. 文件上传 - 后台view 的 Python代码如下: @csrf_exem ...

  5. ASP.NET上传下载文件

    ASP.NET上传下载文件 上传文件: using System; using System.Collections.Generic; using System.Linq; using System. ...

  6. 支持国产ARM64架构部署,支持使用rz、sz命令上传下载文件,JumpServer堡垒机v2.12.0发布

    支持国产ARM64架构部署,JumpServer堡垒机v2.12.0发布 7月19日,JumpServer开源堡垒机正式发布v2.12.0版本.在这一版本中,JumpServer新增支持ARM64架构 ...

  7. springboot上传下载文件(4)--上传下载工具类(已封装)

    因为在做毕设,发现之前的搭建ftp文件服务器,通过ftp协议无法操作虚拟机临时文件,又因为ftp文件服务器搭建的比较麻烦:而 hadoop的HDFS虽然可以实现,但我这里用不到那么复杂的:所以我封装了 ...

  8. CentOS7 网络配置之远程服务器设置、终端登陆和远程上传下载文件

    目录 一.概念 二.远程服务器设置.终端连接和远程上传下载文件 2.1 SSH协议 2.1.1 ssh服务器配置 2.1.2 终端连接 2.1.3 远程上传下载文件 2.2 Telnet协议 2.2. ...

  9. ftp上传-下载文件通用工具类,已实测

    话不多说直接上代码 package com.springboot.demo.utils;import lombok.extern.slf4j.Slf4j; import org.apache.comm ...

最新文章

  1. 谷歌CEO皮查伊「美国版后浪演讲」:You Will Prevail,你们终将胜利
  2. windows修改环境变量神器—Rapid Environment Editor
  3. 哪里可以找到python的免费教程-哪里可以找到比较靠谱的Python教程?
  4. redis的lrange_Redis Lrange 命令
  5. 【Python】学习笔记总结8(经典算法)
  6. boost::multiprecision模块将使用 fixed_int 的算术结果与 GMP 结果进行比较相关的测试程序
  7. unix 只有root 可以使用chown吗_chmod 777 是开发的常规操作吗?
  8. 你们也只剩下点赞的交情
  9. 穆里尼奥:与范加尔风格不同,转变需要时间
  10. Unity3D 游戏引擎之构建游戏地形的基本元素(五)
  11. java jpa是什么_jpa是什么框架?jpa是什么意思?
  12. 探索 Word 2007 开发(二):扩展 Ribbon
  13. Oracle前期准备
  14. 实现导航栏的几种方式
  15. 《设计模式之禅》观后感
  16. docker中安装mongo3.2
  17. 谷歌浏览器和火狐浏览器永久禁用缓存【一劳永逸的解决方式】
  18. 航空管理系统c语言程序设计,2018年北京航空航天大学软件院991数据结构与C语言程序设计之C程序设计考研核心题库...
  19. Android AVB的校验宏观的两个阶段
  20. 计算机大三名企实习怎么找?

热门文章

  1. 程序员面试→如何巧妙的谈薪资?
  2. 2019年7月15日 学习日志
  3. 域控ntp服务器配置,Win1216域控设置NTP服务器
  4. GenZ,CXL,NVLINK,OpenCAPI,CCIX乱战
  5. ipcs 查看IPC 对象信息
  6. jp@gc - Ultimate Thread Group、jp@gc - Response Times vs Threads实践使用
  7. 二十、Gtk4-GtkMenuButton, accelerators, font, pango and gsettings
  8. 深度学习图像融合_深度学习图像分割必看的12篇文章
  9. RS232的硬件流控
  10. mysql自定义变量赋值顺序_MySQL 自定义变量@ 常用案例