C#上传图片Word文件下载.docx
《C#上传图片Word文件下载.docx》由会员分享,可在线阅读,更多相关《C#上传图片Word文件下载.docx(41页珍藏版)》请在冰点文库上搜索。
![C#上传图片Word文件下载.docx](https://file1.bingdoc.com/fileroot1/2023-4/30/cf0ecdc8-289f-4f9c-a864-73c01f0bb1dd/cf0ecdc8-289f-4f9c-a864-73c01f0bb1dd1.gif)
{
}
protectedvoidbt_upload_Click(objectsender,EventArgse)
try
if(FileUpload1.PostedFile.FileName=="
"
)
this.lb_info.Text="
请选择文件!
;
else
stringfilepath=FileUpload1.PostedFile.FileName;
stringfilename=filepath.Substring(filepath.LastIndexOf("
\\"
)+1);
stringserverpath=Server.MapPath("
images/"
)+filename;
FileUpload1.PostedFile.SaveAs(serverpath);
上传成功!
catch(Exceptionex)
上传发生错误!
原因是:
+ex.ToString();
}
前台代码:
<
tablestyle="
width:
343px"
>
tr>
tdstyle="
100px"
单文件上传<
/td>
/tr>
asp:
FileUploadID="
FileUpload1"
runat="
server"
Width="
475px"
/>
ButtonID="
bt_upload"
OnClick="
bt_upload_Click"
Text="
上传"
<
100px;
height:
21px;
LabelID="
lb_info"
ForeColor="
Red"
183px"
/asp:
Label>
21px"
/table>
2.多文件上传
if((FileUpload1.PostedFile.FileName=="
&
&
FileUpload2.PostedFile.FileName=="
)&
FileUpload3.PostedFile.FileName=="
HttpFileCollectionmyfiles=Request.Files;
for(inti=0;
i<
myfiles.Count;
i++)
HttpPostedFilemypost=myfiles[i];
if(mypost.ContentLength>
0)
stringfilepath=mypost.FileName;
mypost.SaveAs(serverpath);
catch(Exceptionerror)
原因:
+error.ToString();
%@PageLanguage="
C#"
AutoEventWireup="
true"
CodeFile="
Default.aspx.cs"
Inherits="
_Default"
%>
!
DOCTYPEhtmlPUBLIC"
-//W3C//DTDXHTML1.0Transitional//EN"
"
http:
//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
htmlXmlns="
//www.w3.org/1999/xhtml"
>
headrunat="
title>
多文件上传清清月儿http:
//blog.csdn.Net/21as.Net/<
/title>
/head>
body>
formid="
form1"
div>
多文件上传<
FileUpload2"
FileUpload3"
一起上传"
/div>
/form>
/body>
/html>
3.客户端检查上传文件类型(以上传图片为例)
后台代码和1.最简单的单文件上传一样;
清清月儿.Net/21as.Nethttp:
//blog.csdn.Net/21as.Net<
scriptlanguage="
Javascript"
functionCheck_FileType()
varstr=document.getElementById("
).value;
varpos=str.lastIndexOf("
."
);
varlastname=str.substring(pos,str.length)
if(lastname.toLowerCase()!
="
.jpg"
lastname.toLowerCase()!
.gif"
alert("
您上传的文件类型为"
+lastname+"
,图片必须为.jpg,.gif类型"
returnfalse;
else
returntrue;
}
/script>
104px"
文件上传判断<
400px"
OnClientClick="
returnCheck_FileType()"
/>
104px;
点击上传时先触发客户端事件Check_FileType;
4.服务器端检查上传文件类型(以上传图片为例)
if(IsAllowedExtension(FileUpload1)==true)
请上传图片"
publicstaticboolIsAllowedExtension(FileUploadhifile)
stringstrOldFilePath="
strExtension="
string[]arrExtension={"
"
.jpeg"
.bmp"
.png"
};
if(hifile.PostedFile.FileName!
=string.Empty)
strOldFilePath=hifile.PostedFile.FileName;
strExtension=strOldFilePath.Substring(strOldFilePath.LastIndexOf("
));
arrExtension.Length;
if(strExtension.Equals(arrExtension[i]))
}
5.服务器端检查上传文件类型(可以检测真正文件名)
其实方法4并不好,因为用户可以把XXX.txt伪装为XXX.jpg。
//清清月儿http:
//blog.csdn.Net/21as.Net
System.IO.FileStreamfs=newSystem.IO.FileStream(hifile.PostedFile.FileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
System.IO.BinaryReaderr=newSystem.IO.BinaryReader(fs);
stringfileclass="
bytebuffer;
buffer=r.ReadByte();
fileclass=buffer.ToString();
fileclass+=buffer.ToString();
catch
r.Close();
fs.Close();
if(fileclass=="
255216"
||fileclass=="
7173"
)//说明255216是jpg;
7173是gif;
6677是BMP,13780是PNG;
7790是exe,8297是rar
6.上传文件文件名唯一性处理(时间戳+SessionID)
年月日时分秒+临时session+原文件名如果大家怕还会重复可以加GUID
try
)+System.DateTime.Now.ToString("
yyy-MM-dd-hh-mm-ss"
)+Session.SessionID+filename;
注:
GUID的方法:
GuidmyGuid=Guid.NewGuid();
7.上传图片生成等比例缩略图
缩略图代码:
ImageThumbnail.cs
usingSystem.IO;
usingSystem.Drawing;
usingSystem.Drawing.Imaging;
publicclassImageThumbnail
publicImageResourceImage;
privateint