该代码只能改一个文件名。不知道是怎么回事?多个就不能改,并且只能传一张。file.SaveAs(uploadPath + file.FileName);成功,不改名file.SaveAs(uploadPath + pic);改名要出错?不知道这里怎么操作,高手指点一下。
<%@ WebHandler Language="C#" Class="UploadHandler" %>using System;
using System.Web;
using System.IO;public class UploadHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        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 (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            
            string imgNameTime = DateTime.Now.ToString("yyyyMMddHHmmss");
            string pic = string.IsNullOrEmpty(file.FileName.Trim()) ? "" : imgNameTime + System.IO.Path.GetExtension(file.FileName); //得到名字+扩展名(例如20001265466.jpg)
            
             //file.SaveAs(uploadPath + file.FileName);
             file.SaveAs(uploadPath + pic);
            //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
            context.Response.Write("1");
        }
        else
        {
            context.Response.Write("0");
        }  
            }
 
    public bool IsReusable {
        get {
            return false;
        }
    }}

解决方案 »

  1.   


    我测试了只要context.Response.Write("1")返加就成功,不返回就失败,0与1感觉没啥用,难道我配置错了?
      

  2.   

    首先if循环只能运行一次,建议用while循环。其次 这句string imgNameTime = DateTime.Now.ToString("yyyyMMddHHmmss");上传速度很快,你只是精确到了秒,建议后面在加点随机字符串,不然的话前面上传的就被覆盖掉了