一、图片
1、在前端用<asp:FileUpload ID="UpImgName" runat="server"/>控件
2、在后台.cs中写上
protected void btnSubmit_Click(object sender,EventArgs e)
{
string strImgPath=string.Empty;
string strDateTime=dateTime.Now.Tostring("yyyyMMddhhmmss");
strImgPath=this.UpImgPath.PostedFile.FileName;
if(strImgPath!="")
{
string extension="";//扩展名
extension=Path.GetExtension(strImgPath).ToLower();
if(extension==".jpg"||extension==".jpeg"||extension==".bmp||extension==""gif")
{
if(this.UpImgPath.PostedFile.ContentLength>1000000)//图片大小是否大于1M
{
Response.Write("<script>alert('图片太大')</script>");
return;
}
strImgPath="/Images/"+strDateTime+extension;
string UpPath=Server.MapPath(strImgPath);
this.UpImgPath.PostedFile.SaveAs(UpPath);
}
else
Response.Write("<script>alert('图片格式错误')</script>");
return ;
}
}
}
二、视频,文件
1、在前端用<asp:FileUpload ID="UpVideoName" runat="server"/>控件
string strVideoPath=string.Empty;
string strDateTime=DateTime.Now.Tostring("yyyyMMddhhmmss");
strVideoPath=this.UpVideoName.PostedFile.FileName;
if(strVideoPath!="")
string extension="";
extension=Path.GetExtension(strVideoPath).ToLower();
if(extension==".flv" || extension == ".doc" || extension == ".docx" || extension == ".zip" || extension == ".rar")
if(this.UpVideoName.PostedFile.ContentLength>30000000)
Response.Write("<script>alert('视频或文件太大')</script>");
return;
strVideoPath="/VideoOrFile/"+strDateTime+extension;
string UpPath=Server.MapPath(strVideoPath);
this.UpVideoName.PostedFile.SaveAs(UpPath);
Response.Write("<script>alert('存储的格式不正确')</script>")
return;
asp.net core 交流群:787464275 欢迎加群交流
如果您认为这篇文章还不错或者有所收获,您可以点击右下角的【推荐】按钮精神支持,因为这种支持是我继续写作,分享的最大动力!
作者:
LouieGuo http://www.cnblogs.com/stulzq声明:原创博客请在转载时保留原文链接或者在文章开头加上本人博客地址,如发现错误,欢迎批评指正。凡是转载于本人的文章,不能设置打赏功能,如有特殊需求请与本人联系!
微信公众号:欢迎关注 QQ技术交流群: 欢迎加群
