本人要用jquery做批量上传,一个upload.aspx用于存放uploadify控件,一个UploadHandler.ashx用于保存图片,同时写入数据库。
  现在有个实在很无解的问题,有时候图片能上传成功,有时候却上传不成功,也没有提示,试了就算只上传一个也有不成功的,而且就算成功了也是有时候能执行oncompelete,有时后不执行,现在代码贴出来给大家看看到底有什么问题。。在线等大家回答。
先是upload.aspx的html:
<%@ Page Language="C#" Debug="True" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>上传图片</title>
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery.uploadify.v2.1.0.min.js"></script>
<script type="text/javascript" src="js/swfobject.js"></script>
<link rel="stylesheet" href="js/uploadify.css" type="text/css" media="screen" />
<link href="css/BasicLayout.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : 'js/uploadify.swf',
'script' : 'UploadHandler.ashx',
'cancelImg' : 'js/cancel.png',
'method' :'GET',
'auto' : false,
'multi' : true,
'folder' : '/PicBase&r='+Math.random()+Math.random,
'fileDesc' : 'doc文件或图片文件',   
'fileExt' : '*.doc;*.gif;*.jpg',   
'sizeLimit': 514000 ,
'simUploadLimit' :10 ,
'onError': function(e,queueId,fileObj,errorObj){
alert(fileObj.name +'上传失败');
},
'onComplete': function(e,queueId,fileObj){
alert(fileObj.name +'上传成功');
}});
});
</script>
</head>
<body style="background-image: url(images/0300.gif); text-align: center" >
<form id="form1" runat="server" method="post">
<div>
<table id="Table1" align="center" border="1" cellpadding="2" cellspacing="2"  
style="height: 58px" width="620" frame="border">
<tr>
<td align="center" background="../images/topbg1.jpg" style="height: 23px; border-top-style: groove; border-right-style: groove; border-left-style: groove; border-bottom-style: groove;">
<font color="#0000ff" face="宋体" size="3"><strong style="border-top-style: groove; border-right-style: groove; border-left-style: groove; border-bottom-style: groove">上传图片</strong></font></td>
</tr>
<tr>
<td align="center" style="height: 23px; border-top-style: groove; border-right-style: groove; border-left-style: groove; border-bottom-style: groove;">
</td>
</tr>
<tr>
<td align="center" style="border-top-style: groove; border-right-style: groove; border-left-style: groove; height: 23px; border-bottom-style: groove">
<asp:Panel ID="Panel5" runat="server" Width="608px">
<table width="100%">
<tr>
<td style="width: 101px; border-right: thin groove; border-top: thin groove; border-left: thin groove; border-bottom: thin groove;">
</td>
<td align="left" style="width: 545px; border-right: thin groove; border-top: thin groove; border-left: thin groove; border-bottom: thin groove;">
  说明:点增加图片按钮可一次上传多张图片,可为每张图片写上一句不超过20个字的描述。单张图片大小不大于500k</td>
</tr>
<tr id="tr1">
<td align="center" style="width: 101px; border-right: thin groove; border-top: thin groove; border-left: thin groove; border-bottom: thin groove;">
  请选择图片:</td>
<td align="left" id='td1' style="width: 545px; border-right: thin groove; border-top: thin groove; border-left: thin groove; border-bottom: thin groove;">
  <br />
<input id="uploadify" type="file" name="uploadify" style="width: 500px" /><br />
  &nbsp;
</td>
</tr>
<tr>

解决方案 »

  1.   

    然后是upload.aspx.cs文件:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;  
    using System.IO;public partial class upload : System.Web.UI.Page
    {
        
      protected void Page_Load(object sender, EventArgs e)
      { int flag=0;// int type=1;
        
      document.Text =Request.QueryString["documentno"].ToString();
      // btnUpload.Attributes.Add("onclick", "return iscorrect()");
      ddlAlbum.Text = Session["usercode"].ToString().Substring(0, 8);
      if (document.Text.Substring(0, 1) == "P") {
      note.Text = "承保"; flag = 1;  }
      else if (document.Text.Substring(0, 1) == "A") { note.Text = "理赔"; flag = 3; }
      else {
      note.Text = "防灾防损"; flag = 2;
      }
        
      string insertstr = "";
      insertstr = document.Text;
      Session["insertstr"] = insertstr;
      string script = "$('#uploadify').uploadifyUpload()";
      // Response.Write("<script>alert('" + Session["insertstr"] .ToString()+ "')</script>");
      btnUpload.Attributes.Add("onclick", script);
      }
      

  2.   

    最后是UploadHandler.ashx:
    <%@ WebHandler Language="C#" Class="UploadHandler" %>using System;
    using System.IO;
    using System.Net;
    using System.Web;
    using System.Data.Sql;
    using System.Data.SqlTypes;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    public class UploadHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState{  public void ProcessRequest(HttpContext context) {
      int flag=0;
      int type=0;
      // HttpContext context=HttpContext.Current;
      try
      {
      context.Response.ContentType = "text/plain";
      context.Response.ContentEncoding = System.Text.Encoding.UTF8;
      context.Request.ContentEncoding = System.Text.Encoding.UTF8;
      if (context.Session["insertstr"] != null)
      {
        
      string PicPath = "";
      string comcode =context.Session["usercode"].ToString().Substring(0,8);  string documentno = context.Session["insertstr"].ToString();
      if (documentno.Substring(0, 1) == "P")
      {
      flag = 1;  }
      else if (documentno.Substring(0, 1) == "A") { flag = 3; }
      else
      {
      flag = 2;
      }
      string dirname = comcode+documentno;
      string dirpath =context.Server.MapPath("./PicBase");
      dirpath = dirpath + "\\" + dirname;
      dirpath = dirpath + DateTime.Now.ToString("yyyy-MM-dd");
      if (Directory.Exists(dirpath) == false)
      {
      Directory.CreateDirectory(dirpath);
      }
      Random ro = new Random();
      int name = 100;  for (int i = 0; i < context.Request.Files.Count; i++)
      {
      name = name + i;
      string FileExtention = "";
      string date = DateTime.Now.ToString("yyyy-MM-dd-HH");
      HttpPostedFile myFile = context.Request.Files[i];
      string FileName = Path.GetFileName(myFile.FileName);
      string NewName = "";
      if (FileName != "")
      {   
      FileExtention = Path.GetExtension(myFile.FileName);
      // postedFile.SaveAs(fileName);
      if (FileExtention != ".doc" && FileExtention != ".DOC")
      {
      type = 1;
      string stro = comcode+FileName.Substring(FileName.IndexOf("\\") + 1, FileName.IndexOf(FileExtention))+date + name.ToString(); NewName = stro;
      }
      else
      {
      type = 2;
      NewName = comcode + FileName.Substring(FileName.IndexOf("\\") + 1, FileName.IndexOf(FileExtention)) + DateTime.Now.ToString("yyyy-MM-dd-HH")+name.ToString();
      // Response.Write("<script>alert('"+NewName+" ')</script>");
      }
      string ppath = dirpath + "\\" + NewName + FileExtention;
      myFile.SaveAs(ppath);
      // myFile.SaveAs(ppath);
      string FJname = FileName;
      PicPath = "PicBase" + "\\" + dirname + DateTime.Now.ToString("yyyy-MM-dd") + "\\" + NewName + FileExtention;
      //TextBox1.Text = PicPath.ToString();
      Console.WriteLine(FileName);
      AddPicture(PicPath, FileName.Substring(FileName.IndexOf("\\") + 1, FileName.IndexOf(FileExtention)), comcode, DateTime.Now, documentno, flag, type);//将图片
      }  
      }
      }
      context.Response.Write("");
      }
      catch (Exception ex)
      {
      context.Response.ContentType = "text/plain";
      context.Response.Write(ex.Message);
      }
      }   
      private void AddPicture(string imgpath, string imgnote, string albumid, DateTime time, string documentno,int flag,int type)
      {  string sql = "insert WB_AlbumImges(ImgPath,ImgNote,AlbumID,time,documentno,flag,type) values('" + imgpath + "','" + imgnote + "','" + albumid + "','" + time + "','" + documentno + "','" + flag + "','" + type + "')";
      string ads = "Data Source=40.177.31.253;Initial Catalog=piccimage;User ID=sa;Password=abc123;";
      SqlConnection mydb = new SqlConnection(ads);
      SqlCommand cmd = new SqlCommand(sql);
      mydb.Open();
      cmd.Connection = mydb;
      cmd.ExecuteNonQuery();  mydb.Close();  }
        
         public bool IsReusable {
      get {
      return false;
      }
      }
       }
    大家救救我吧。
      

  3.   

    百度搜索,应该是upload的文件最大上传限制,不过即使增加了文件上传大小容量,效果也不好,还会刷新页面。
    解决思路:去网上下载NeatUpload组件,下载后的文件(或者解压后)是一套组件,有DLL和JS等文件,直接用到项目中,改一改webconfig就行。这个组件无刷新,最大容量和强,因为服务器关系,我在项目中只测试了一个G的文件上传,文件上传没有页面刷新,而且带有进度条,上传样式可以改其中的CSS文件,具体动作修改其中的JS文件,组件下载后的文件中有详细的说明。