看下这个例子我随手写的前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mx.aspx.cs" Inherits="mx" %><!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
        <table style="width: 488px; height: 192px">
            <tr>
                <td style="width: 100px; height: 13px">
                    姓名:</td>
                <td style="width: 100px; height: 13px">
                    <input id="Text1" runat="server" type="text" /></td>
                <td style="width: 100px; height: 13px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px; height: 10px">
                    感言:</td>
                <td style="width: 100px; height: 10px">
                    <textarea id="TextArea1" runat="server" style="width: 336px; height: 96px"></textarea></td>
                <td style="width: 100px; height: 10px">
                </td>
            </tr>
            <tr>
                <td rowspan="2" style="width: 100px">
                    图片:</td>
                <td style="width: 100px; height: 144px">
                    &nbsp;<img id="IMG1" runat="server" src="" /></td>
                <td style="width: 100px; height: 144px">
                </td>
            </tr>
            <tr>
                <td style="width: 100px; height: 15px">
                    <input id="File1" runat="server" type="file" /></td>
                <td style="width: 100px; height: 15px">
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></td>
            </tr>
        </table>
    </form>
</body>
</html>
后台
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.Text;
public partial class mx : System.Web.UI.Page
{
    jxcapital.DBConn conn = new jxcapital.DBConn();
    DataSet ds = new DataSet();
    StringBuilder sd = new StringBuilder();    protected void Page_Load(object sender, EventArgs e)
    { 
        string strsql = ""; 
        string id=Request.QueryString["id"];
        strsql = "select [name],[body],[url],[jl] from mx where id='"+ id.ToString() +"'";
        ds = conn.ExecuteQuery(strsql);
        conn.Close();
        this.Text1.Value = ds.Tables[0].Rows[0].ItemArray[0].ToString();
        this.TextArea1.Value=ds.Tables[0].Rows[0].ItemArray[1].ToString();
        IMG1.Src = ds.Tables[0].Rows[0].ItemArray[2].ToString();       
    }    protected void Button1_Click(object sender, EventArgs e)
    {   
        string id=Request.QueryString["id"];
        string strFileName = this.File1.PostedFile.FileName;
        int nLength = strFileName.Length - (strFileName.LastIndexOf("\\") + 1);
        strFileName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1,nLength);
        //string strpath = Server.MapPath("\\") + "upimg\\";
        string strpath = MapPath("down") + "\\";
        File1.PostedFile.SaveAs(strpath + strFileName);
        
        
        strsql="update [mx] set name='" + name.ToString() + "',name='" + name.ToString() + "' ,where id='" + id.ToString() + "'"
       
    }
    protected void ImageMap1_Click(object sender, ImageMapEventArgs e)
    {    }
}
在项目中建一个down的文件夹

解决方案 »

  1.   

    Page_Load和前面的声明 
    jxcapital.DBConn conn = new jxcapital.DBConn();
        DataSet ds = new DataSet();
        StringBuilder sd = new StringBuilder();不要
    只要 Button1_Click这段是上传
      

  2.   

    我在没有使用那个Ajax之前也是可以上传的哦!现在主要是用了这个Ajax之后,就获取不到imgUpload.PostedFile.FileName 这个的值!!!!
      

  3.   

    cs里的这句
    if  (imgUpload.PostedFile.FileName  !="")  
    换成
    if(Request["imgUpload"] != null && Request["imgUpload"].ToString() != "")
    试一下
      

  4.   

    那我也不知道了...下班了,周一有空试试看是不是AJAX不能上传了,怪异
      

  5.   

    高手使用ajax如何上传图片呢??
      

  6.   

    ajax是将需要与服务器交互的数据序列化为XML格式的流,传给服务器。而上传的文件不能够被序列化为这种格式,所以不行。楼主也不用试了,如果想实现无刷新的效果,就用Iframe吧
      

  7.   

    搜到这个问题
    也想用ajax上传图片
    看样子现在没有招呀
    有没有什么什么可以用的组件什么,直接实现异步上传呢
      

  8.   

    上传图片必须提交文件的数据流,这个无法用AJAX处理的,所以必须PostBack回去还有,AJAX使用XMLHttpRequest,和原来页面的Request不一样,所以在AJAX方法中也无法访问别的控件
      

  9.   

    ajax的确没办法实现无刷新图片上传。
      

  10.   

    上传组件 与 Ajax 是不兼容的,
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <Triggers>
                <asp:PostBackTrigger ControlID="DV1" />
            </Triggers>
        </asp:UpdatePanel>所以只能在 updatepanel 中加了PostBackTrigger 使上传时触发 整页的post
    除DV1之外的还是 部分更新
    http://www.mybuffet.cn