.net 上传文件

来源:互联网 发布:adobe voco mac 编辑:程序博客网 时间:2024/04/28 11:33

页面部分用 JQuery 来验证

 

 <script language="javascript" type="text/javascript">
           function CheckImage(obj){
                var fileName = $(obj).val();
                if (fileName !=""){
                    var exName = fileName.substr(fileName.lastIndexOf(".") + 1).toUpperCase()
                    if (exName == "JPG" || exName == "BMP" || exName == "GIF"||exName=="PNG") {
                    } else {
                        alert("请选择正确的图片文件!");
                        var prevobj = $(obj).parent("td");
                        prevobj.empty();
                        prevobj.append("<input id='upfileimg' class='input' type='file' name='imagefile' onchange='CheckImage(this)'>");
                    }
               }
           }
           function CheckFileNull(){
               var fileName=$("#upfileimg").val();
               if(fileName==""){
                      return false;
               }else{
                      return true;
               }
           }
      </script>

 

C# 代码上传的使用方法

 

System.Web.HttpPostedFile fileannex = Request.Files["articleannex"];

public bool FileUpload(System.Web.HttpPostedFile fileannex)
{
 ArticleAnnexInfo articleannexInfo =null;
 if (fileannex != null && fileannex.FileName.Length >= 1)
                {
                    articleannexInfo = new ArticleAnnexInfo();
                    if (!CheckExName(System.IO.Path.GetExtension(fileannex.FileName))) //非法文件禁止上传
                    {
                        return false;
                    }
                    try
                    {
                        int fileSize = fileannex.ContentLength;
                        string fileType = fileannex.ContentType;
                        string fileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(fileannex.FileName);
                        string filePath = Server.MapPath("/files/articleannex");
                        string fullName = System.IO.Path.Combine(filePath, fileName);

                        if (!System.IO.Directory.Exists(filePath))
                            System.IO.Directory.CreateDirectory(filePath);
                        fileannex.SaveAs(fullName);

                        articleannexInfo.FileName = fileannex.FileName.Substring(fileannex.FileName.LastIndexOf("//") + 1);
                        articleannexInfo.FilePath = "/files/articleannex/" + fileName;
                        articleannexInfo.FileSize = fileSize;
                        articleannexInfo.FileType = fileType;
                    }
                    catch
                    {
                        articleannexInfo = null;
                    }
                }
}

/////检查是否是合法文件

 private bool CheckExName(string str)
        {
            bool ret = false;
            string[] exstr = new string[]{ ".txt", ".doc", ".rar", ".zip", ".xls", ".pdf" };
            foreach (string tempstr in exstr)
            {
                if (string.Compare(str, tempstr, true) == 0)
                {
                    ret = true;
                    break;
                }
            }
            return ret;
        }