做一个文件在线客服的上传功能,原来是P2p的,先改成服务器中转的。
HTML:
<input style="border:solid 1px black;" type="file" id="fileup" name="houseMaps" size="30" runat="server"/>
// 没加这个runat=server也可以上传
<input style="border:solid 1px black;" type="button" value="上传" onclick="ajaxFileUpload();"/>
javascript部分:
function ajaxFileUpload() {
    var fileObj = document.getElementById("houseMaps");
    if (fileObj.value = null || fileObj.value == "") {
        alert("请选择要上传的文件!");
        return;
    }
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
    xmlHttp = new XMLHttpRequest();
}
    xmlHttp.onreadystatechange = callback;
    xmlHttp.open("GET", "ajaxUpFile.ashx?filepath=" + fileObj.value, true);
    xmlHttp.send(null);}
function callback() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            var fileObj = document.getElementById("fileup");
            var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "false";
            xmlDoc.loadXML(xmlHttp.responseXML.xml);
            var item1 = xmlDoc.getElementsByTagName("data1"); //加密后的文件名
            var item2 = xmlDoc.getElementsByTagName("data2"); //未加密的文件名
            var res1 = item1[0].text;
            var res2 = item2[0].text;
            //省略
          }
    } else {
        //
    }
}
HttpHandler 部分关键代码:
                    FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                    BinaryReader r = new BinaryReader(fs);
                    WebClient myWebClient = new WebClient();
                    string s = System.DateTime.Now.ToString("yyyyMMddHHmmss");
                    string toFile = context.Server.MapPath("~/UploadFile/" + s + mname + extname);
                    //设定windows网络安全认证
                    myWebClient.Credentials = CredentialCache.DefaultCredentials;
                    byte[] postArray = r.ReadBytes((int)fs.Length);
                    Stream postStream = myWebClient.OpenWrite(toFile);
                    if (postStream.CanWrite)
                    {
                        postStream.Write(postArray, 0, postArray.Length);
                        string encryptedfile = (s + mname + extname);//Encrypt(s + mname + extname);
                        context.Response.ContentType = "text/xml";
                        context.Response.Write("<?xml version='1.0' encoding='GB2312' ?>");
                        context.Response.Write("<data><data1><encryptedfile>" + encryptedfile + "</encryptedfile></data1><data2><file>" + s + mname + extname + "</file></data2></data>");
}
form表单加了entype属性在httphandler里也接收不到文件的信息,只能以这样的方式读取文件,然后上传的服务器。测试在IIS和VS的调试服务器都可以实现上传文件,但是遇到一个很严重的问题:
在别的机器上(局域网)访问这个网站就不能上传了,奇怪!在那台机器上选择一个文件后上传,报错是找不到文件呢?貌似只能上传服务器端的文件,为什么不能接受客户端的呢?
第一次提问,请高手指点一下。

解决方案 »

  1.   

    fileupload上传很方便
    filepath
    获取文件流把Form给POST到服务器端去
      

  2.   

    参考: 
       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);
                }
                file.SaveAs(uploadPath + file.FileName);
                  context.Response.Write("1");
            }
            else
            {
                context.Response.Write("0");
            }
        } 
      

  3.   

    to mngzilin:
     在Httphandler 里面可以这么做吗,我试过了接收不到的啊?
      

  4.   

    楼主上传的东西大吗?不大的话
    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);
                }
                file.SaveAs(uploadPath + file.FileName);
                  context.Response.Write("1");
            }
            else
            {
                context.Response.Write("0");
            }
        } 
    已经满足啦。