我网上找了一个上传图片的方法,是用jquery和ajax实现无刷新上传。通过一个ashx文件来处理图片上传,在ashx文件中对上传文件进行了重命名。在图片上传页面使用如下脚本:
<script type="text/javascript">
        function upLogo() {
            $.ajax
                        ({
                            type: "POST",
                            url: "AjaxUpPIC.ashx",
                            data: { upfile: getPath($("#File")) },
                            success: function (data) {
                                alert(data);
                               
                            }
                        });
        }
 </script>
来上传,我试过了,可以上传。可是怎么能在页面后台中得到更改后的文件名呢

解决方案 »

  1.   

    上传时文件名字更改在那里实现?在jquery中的话,就把名字作为参数传到ajaxuppic.ashx页面,再保存到数据库中,在ajaxuppic.ashx中改名直接保存
      

  2.   

    AjaxUpPIC.ashx 上传成功返回的是 data吧?
    你看看 把返回值改成图片名称就然后前台alert一下看看
    我也不太熟悉ajax 好像data就是成功后的返回值
      

  3.   

    是在ashx中改的名字,除了名字外,还有其他实体信息也要保存,所以我需要在上传页面中得到在ashx更改后的文件名
      

  4.   

    嗯 是的,更改后的文件名在data中可以得到,在点上传后我将文件名放到一个lable中。可是我在点提交后,就得不到这个值了
      

  5.   

    文件名在
    "AjaxUpPIC.ashx
    里面
    context.Response.Write()出来
      

  6.   

    已经write,怎么在上传页面获取呢?
      

  7.   

    success: function (data) {
      alert(data);
       
      }
    这里alert出来的是什么?
      

  8.   

    这个alert出来的是我要的文件名,我把它放到一个lable中,在后台我要操作存入库中。可是后台得到的lable.text是空值
      

  9.   

     string   filename=filename.substring(filename.lastindexof("\\")+1);文件名
      

  10.   


    lbl解析以后是span标签,ID不对吧。。
    那代码出来看看。。
      

  11.   

    那是你放到label中是错误的。
    success: function (data) {
      document.getElementById("<%=Label1.ClientID%>").innerHTML= data;
       
      }
      

  12.   

    客户端没有text这样的属性的,只能innerHTML
      

  13.   

    非常感谢这位大哥!那我在后台怎么取出来这个 innerhtml呢?
      

  14.   

    关键看你这么做的目的是什么,如果只是把文件名显示给人看,那么在前面innerHTML,如果是后台处理的时候需要用到,那就在AjaxUpPIC.ashx里面使用就行了。前台还要取用文件名进行处理,那么只能用js来调用处理,而不能用直接用后台处理。
      

  15.   

    用上传控件啊····public bool SaveCover()
            {
                String savePath = Server.MapPath("/BookCover/");
                if (FileUpload1.HasFile)
                {
                    // Get the name of the file to upload.
                    String fileName = FileUpload1.FileName;
                    int fileSize = FileUpload1.PostedFile.ContentLength;
                    string extension = System.IO.Path.GetExtension(fileName);
                    if (!(extension == ".jpg" || extension == ".JPG"))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('图片格式错误,应为.jpg格式!图书信息保存失败!')", true);
                        return false;
                    }
                    int index = fileName.LastIndexOf(".");
                    string type = fileName.Substring(index).ToLower();//取文件的扩展名
                    string newName = System.DateTime.Now.ToString("yyyyMMddHHmmssffff");//声称文件名,防止重复
                    newName = newName + type;
                    if (fileSize < 2097152)
                    {                    // Append the name of the file to upload to the path.
                        savePath += newName;
                        // Call the SaveAs method to save the 
                        // uploaded file to the specified path.
                        // This example does not perform all
                        // the necessary error checking.               
                        // If a file with the same name
                        // already exists in the specified path,  
                        // the uploaded file overwrites it.
                        FileUpload1.SaveAs(savePath);                    // Notify the user of the name of the file
                        // was saved under.
                        //txtCoverPhoto.Text = "/BookCover/" + newName;
                        CoverPath = "/BookCover/" + newName;
                        //imgCoverPhoto.ImageUrl = txtCoverPhoto.Text.Trim();
                        //Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('图书照片上传成功!')", true);
                        return true;
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('图书照片超过2M,上传失败!图书信息保存失败!')", true);
                        return false;
                    }
                }
                else
                {
                    // Notify the user that a file was not uploaded.
                    //txtIntro.Text = "You did not specify a file to upload.";
                    //Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('图书照片上传失败!图书信息保存失败!')", true);
                    return true ;
                }
            }