context.Response.ContentType = "text/plain"; context.Response.Charset = "utf-8"; HttpPostedFile file = context.Request.Files["Filedata"]; string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\"; if (file != null) { //if (File.Exists(uploadPath + file.FileName)) //{ // context.Response.Write("3"); //文件已经存在 // return; //} string[] fn = file.FileName.Split('.'); string ext = fn[fn.Length - 1]; string filename = DateTime.Now.ToString("yyyyMMddhhmmss")+"."+ext; if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } file.SaveAs(uploadPath + filename); //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失 context.Session[context.Session["userName"].ToString()] = filename; context.Response.Write(filename); } else { context.Response.Write("0"); }
成功上传返回文件的名字,失败的话返回一个0,js不追返回值,如果是0表示失败,如果不是0则动态的给img加载src。
源码下载:(jb51.net).rar
您可能感兴趣的文章: