|
限制上传的实例,限制大小可以在 web.config里设置,上次我在谷歌上搜到了。- //上传附件
- if (!string.IsNullOrEmpty(FileUpload1.FileName))
- {
- fileName = Path.GetFileName(FileUpload1.FileName);
- fileCode = DateTime.Now.Ticks.ToString();
- if (FileUpload1.HasFile)
- {
- String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
- String[] allowedExtensions = { ".exe", ".bat", ".cmd" };
- for (int i = 0; i < allowedExtensions.Length; i++)
- {
- if (fileExtension == allowedExtensions[i])
- {
- fileOK = false;
- }
- }
- }
- if (fileOK == true)
- {
- try
- {
- FileUpload1.SaveAs(Server.MapPath("~/Upload/Files/" + fileName));
- if (Page.Request.QueryString["id"] != null)
- ikgAttach.Add(Page.Request.QueryString["id"], fileName, fileCode, desc);
- else
- ikgAttach.Add("", fileName, fileCode, desc);
- ExtAspNet.Alert.Show("保存成功!", string.Empty, ExtAspNet.ActiveWindow.GetHidePostBackReference());
- }
- catch (Exception ex)
- {
- ExtAspNet.Alert.ShowInParent(ex.Message, ExtAspNet.MessageBoxIcon.Error);
- }
- }
- else
- {
- ExtAspNet.Alert.ShowInParent("此文件类型被限制上传!", ExtAspNet.MessageBoxIcon.Error);
- }
- }
复制代码 |
|