一.上传扫描件到服务器,自定义创建文件夹(如果存在该文件夹,则无需创建),并判断格式以及文件大小进行保存:
首先创建一个保存按钮事件:
protected void btnSave_Click(object sender, EventArgs e)
{
try
{#region 增值税专用发票扫描件 验证
if (UploadFile1.PostedFile == null || UploadFile1.PostedFile.ContentLength <= )
{
tools.showMsg("请选择“增值税专用发票”扫描件信息!", true, null);
this.UploadFile1.Focus();
return;
}
if (UploadFile1.PostedFile.ContentLength > * )
{
tools.showMsg("“增值税专用发票”扫描件大于250K,不能上传!", true, null);
this.UploadFile1.Focus();
return;
} #endregion #region 上传扫描件 string strErrMsg = "";
string conType = this.txtCorpName.Value; //获取将要存储的文件夹名称
bool bl = UploadFile(UploadFile1,conType, out strErrMsg); //tb_corpName.Text.Trim()
if (!bl)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('“增值税专用发票”扫描件上传失败')</script>");
this.UploadFile1.Focus();
return;
}
#endregion }
catch(Exception ex)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('"+ex.Message+"')</script>");
}
}
调用 UploadFile 方法:
private bool UploadFile(HtmlInputFile inputfile,string conType, out string strErrMsg) //传入三个参数:file控件,文件夹名称,错误信息
{ strErrMsg = "";
string strUploadFileName = "";
try
{
if (inputfile.PostedFile != null)
{
if (inputfile.PostedFile.ContentLength > ) //判断是否选中文件
{
string strUploadFolder = Server.MapPath("..\\UploadAffix\\" + conType.ToString()); //获取将要创建的文件夹名称 if (!Directory.Exists(strUploadFolder)) //判断是否已创建 (如果未创建,则进行创建)
{
Directory.CreateDirectory(strUploadFolder);
} string strExtName = Path.GetExtension(inputfile.PostedFile.FileName); //判断上传文件的类型
if (strExtName.ToUpper() != ".JPG" && strExtName.ToUpper() != ".JPEG")
{
strErrMsg = "请选择扫描件文件(*.JPG,*.JPEG)!";
return false;
} strUploadFileName = Server.MapPath(@"..\");
string strGUID = Guid.NewGuid().ToString("B"); //生成唯一的标识符
strUploadFileName += @"UploadAffix" + @"\" + conType + @"\" + strGUID;
strUploadFileName += strExtName;
inputfile.PostedFile.SaveAs(strUploadFileName); jpgStrPath = "/UploadAffix/" + conType + "/" + strGUID + strExtName; //获取保存在文件夹下的路径
return true;
}
else
{
strErrMsg = "请选择要上传的扫描件文件!";
return false;
}
}
else
{
return false;
}
}
catch (Exception ex)
{
strErrMsg = "上传扫描件文件出现问题:" + ex.Message;
return false;
}
}
二.通过FileUploadButton按钮上传文件,实现将文件保存到服务器。
后台代码:
protected void FileUploadButton_Click(object sender, EventArgs e)
{
bool fileOk = false; string fileName = this.MyFileUpload.FileName; //获取上传的文件名 int fileSize = MyFileUpload.PostedFile.ContentLength; //获取上传文件大小(25600) string path = Server.MapPath("~\\UploadFiles\\"); //指定文件路径,是项目下的一个文件夹;~表示当前网页所在的文件夹 if (MyFileUpload.HasFile) //判断是否选中文件
{
string fileExtension = System.IO.Path.GetExtension(MyFileUpload.FileName).ToLower(); //得到文件后缀 string[] allowedExtensions = { ".jpg", ".jpeg" }; //允许的得到的后缀 for (int i = ; i < allowedExtensions.Length; i++) //看包含的文件是否是被允许的文件的后缀
{
if (fileExtension == allowedExtensions[i])
{
fileOk = true;
}
}
if (!fileOk) //判断格式是否为jpg或jpeg
{
Response.Write("<script>alert('只能上传jpeg,jpg图象文件!');</script>");
}
if (fileSize > ) //如果文件大于250K 则提示不能上传
{
Response.Write("<script>alert('文件最大为250KB!');</script>");
return;
}
if (fileOk)
{
try
{
string newName = fileName+DateTime.Now.ToString("yyyyMMddhhmmss");//新文件名
MyFileUpload.PostedFile.SaveAs(path + newName); //文件另存在服务器的指定目录下
Response.Write("<script>alert('文件上传成功!');</script>");
}
catch(Exception ee)
{
Response.Write("<script>alert('"+ee.Message+"');</script>");
}
}
}
else {
Response.Write("<script>alert('请选择将要上传的文件!');</script>");
} }
前台代码:
<asp:FileUpload ID="MyFileUpload" runat="server" style="width:303px;" />
<asp:Button ID="FileUploadButton" runat="server" Text="上传" OnClick="FileUploadButton_Click" />