zoukankan
html css js c++ java
图片上传/下载,DOC.xls文档下载的方法
上传:
Code
#region
上传文件
protected
string
UpPicFile(System.Web.UI.WebControls.FileUpload FileUpControl,
ref
string
fileType)
{
if
(FileUpControl.HasFile)
{
string
strErr
=
""
;
#region
验证
if
(FileUpControl.PostedFile
==
null
)
{
strErr
+=
"
对不起,上传文件不能为空!\\n
"
;
}
int
size
=
FileUpControl.PostedFile.ContentLength;
//
大小
if
(size
<
1
)
{
strErr
+=
"
对不起,上传文件不能为空!\\n
"
;
}
if
(size
>
10485760
)
{
strErr
+=
"
对不起,文件大小不能大于10M!\\n
"
;
}
if
(strErr
!=
""
)
{
QDHotel.Common.MessageBox.Show(
this
, strErr);
return
null
;
}
#endregion
string
UploadFileType
=
FileUpControl.PostedFile.ContentType;
string
UpFileName
=
FileUpControl.FileName;
string
picname
=
DateTime.Now.ToString(
"
yyyyMMddHHmmss
"
)
+
UpFileName;
fileType
=
UploadFileType;
#region
不同类型UploadFileType
switch
(UploadFileType)
{
case
"
image/gif
"
:
case
"
image/bmp
"
:
case
"
image/pjpeg
"
:
{
Stream StreamObject
=
FileUpControl.PostedFile.InputStream;
//
建立数据流对像
System.Drawing.Image myImage
=
System.Drawing.Image.FromStream(StreamObject);
int
w
=
myImage.Width;
int
h
=
myImage.Height;
}
break
;
case
"
application/msword
"
:
case
"
application/vnd.ms-excel
"
:
break
;
default
:
strErr
+=
"
对不起,不允许该文件格式上传!\\n
"
;
break
;
}
#endregion
if
(strErr
!=
""
)
{
QDHotel.Common.MessageBox.Show(
this
, strErr);
return
null
;
}
try
{
string
path
=
ADUploadFolder
+
picname;
path
=
Server.MapPath(path);
FileUpControl.PostedFile.SaveAs(path);
return
picname;
}
catch
//
(Exception ex)
{
return
null
;
}
}
else
{
return
null
;
}
}
#endregion
下载
private
void
ViewImage(
string
fileType,
string
fileName,
bool
forceDownload)
{
if
(fileType
==
"
application/msword
"
||
fileType
==
"
application/vnd.ms-excel
"
)
{
//
Response.Redirect("UploadPic/" + fileName);
//
return;
System.IO.FileStream r
=
new
System.IO.FileStream(Server.MapPath(
"
UploadPic/
"
+
fileName), System.IO.FileMode.Open);
//
设置基本信息
Response.Buffer
=
false
;
Response.AddHeader(
"
Connection
"
,
"
Keep-Alive
"
);
Response.ContentType
=
"
application/octet-stream
"
;
Response.AddHeader(
"
Content-Disposition
"
,
"
attachment;filename=
"
+
System.IO.Path.GetFileName(Server.MapPath(
"
UploadPic/
"
+
fileName)));
Response.AddHeader(
"
Content-Length
"
, r.Length.ToString());
while
(
true
)
{
//
开辟缓冲区空间
byte
[] buffer
=
new
byte
[
1024
];
//
读取文件的数据
int
leng
=
r.Read(buffer,
0
,
1024
);
if
(leng
==
0
)
//
到文件尾,结束
break
;
if
(leng
==
1024
)
//
读出的文件数据长度等于缓冲区长度,直接将缓冲区数据写入
Response.BinaryWrite(buffer);
else
{
//
读出文件数据比缓冲区小,重新定义缓冲区大小,只用于读取文件的最后一个数据块
byte
[] b
=
new
byte
[leng];
for
(
int
i
=
0
; i
<
leng; i
++
)
b[i]
=
buffer[i];
Response.BinaryWrite(b);
}
}
r.Close();
//
关闭下载文件
Response.End();
//
结束文件下载
}
Response.Clear();
if
(forceDownload)
{
Response.AppendHeader(
"
Content-Disposition
"
,
"
attachment; filename=
"
+
fileName);
}
else
{
Response.AppendHeader(
"
Content-Disposition
"
,
"
filename=
"
+
fileName);
}
using
(System.Drawing.Image image
=
System.Drawing.Image.FromFile(MapPath(picDic
+
fileName)))
{
if
(image.RawFormat.Equals(ImageFormat.Bmp))
{
Response.ContentType
=
"
image/bmp
"
;
}
else
{
if
(image.RawFormat.Equals(ImageFormat.Gif))
{
Response.ContentType
=
"
image/gif
"
;
}
else
{
if
(image.RawFormat.Equals(ImageFormat.Jpeg))
{
Response.ContentType
=
"
image/jpeg
"
;
}
else
{
if
(image.RawFormat.Equals(ImageFormat.Png))
{
Response.ContentType
=
"
image/png
"
;
}
else
{
Response.ContentType
=
"
application/octet-stream
"
;
}
}
}
image.Save(Response.OutputStream, image.RawFormat);
}
}
}
查看全文
相关阅读:
Golang error 的突围
深度解密Go语言之 scheduler
深度解密Go语言之channel
如何打造一份优雅的简历?
Go 程序是怎样跑起来的
曹大谈内存重排
从零开始使用 Webpack 搭建 Vue 开发环境
纯样式无脚本无图片自定义单/复选框
从零开始使用 Webpack 搭建 Vue3 开发环境
JS遍历对象的几种方法
原文地址:https://www.cnblogs.com/weichuo/p/1206620.html
最新文章
EF用导航属性遍历从表时,删除主表出错
EF新增实体后没有导航属性
GitHub克隆或下载时网络慢,导致提示失败-网络错误
修改css后浏览器自动更新样式(__browserLink_refresh)
echarts图表Cannot read property 'calculable' of null
如何实现当后台数据刷新时,实时更新前台界面
面向对象存储框架:Obase快速入门
使用三台云服务器搭建真正的Redis集群
eureka启动I/O error on POST request for "http://localhost:9411/api/v2/spans"
Vue重置数据
热门文章
Vue组件使用百度Ueditor遇到OffSeetWidth为null的错误-解决办法
Cglib和jdk动态代理的区别
JAVA和C#中数据库连接池原理与应用
.Net Core WebApi在Linux上启动和关闭
Vue请求第三方接口跨域最终解决办法!2020最终版!
IOCAutofac与ORMEntityFramwork的联系--单例模式
深度解密 Go 语言之 sync.map
深度解密 Go 语言之 sync.Pool
defer 链如何被遍历
深度解密Go语言之 pprof
Copyright © 2011-2022 走看看