使用ajax和asmx服务上传文件后发布不起作用?

时间:2015-03-15 08:57:03

标签: jquery asp.net web-services asp.net-ajax asmx

[WebMethod] 
public string SaveUpload()//(string u,byte fileData) 
{
        //----------------working-----------
      string response = string.Empty;
      if (HttpContext.Current.Request.Form.AllKeys.Any())
      {
      string USERID =   HttpContext.Current.Request.Form["UploadedImage[0]"];
      string TEXT = HttpContext.Current.Request.Form["UploadedImage[1]"];
      string LINK = HttpContext.Current.Request.Form["UploadedImage[2]"];
      var FILE = HttpContext.Current.Request.Files["UploadedImage[3]"];
      string USERNAME = HttpContext.Current.User.Identity.Name;
      string newfilename = string.Empty;

            if (FILE != null)
            {
                // Validate the uploaded image(optional)
                bool imageOk = CheckImageExtension(FILE.FileName);
                if (imageOk)
                {
                    string time = SqlQuery.SelectQuery("select CURRENT_TIMESTAMP").Tables[0].Rows[0][0].ToString();
                    newfilename = FormsAuthentication.HashPasswordForStoringInConfigFile(USERID + time + FILE.FileName, "SHA1") + ".png";
                    FILE.SaveAs(Server.MapPath("~/Photos/" + newfilename));
                }
                else
                {
                    //incorrect format
                }

                Tpost objTpost = new Tpost();
                objTpost.Text = System.Web.HttpUtility.HtmlEncode(TEXT);
                objTpost.Photo1 = newfilename;// uploadPhoto.FileName;
                objTpost.ByUser = HttpContext.Current.User.Identity.Name;
                objTpost.UserID = USERID;
                objTpost.Link = System.Web.HttpUtility.HtmlEncode("http://" + LINK.Replace("http://", ""));
                objTpost.Status = 0;
                objTpost.Mode = 1;
                DataTable dt = objTpost.GetPost();
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                ds.Tables[0].TableName = "Customers";
                response = ds.GetXml();

                // Get the complete file path
                //var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Photos"), FILE.FileName);

                // Save the uploaded file to "UploadedFiles" folder
                //httpPostedFile.SaveAs(fileSavePath);
            }

        }
        return response;
    }

上面是用serve.asmx写的服务函数

  $(document).on('click', "#pbtnupload", function (e) {
    e.preventDefault();
    var data = new FormData();
    data.append("UploadedImage[0]", arr[0]);
    data.append("UploadedImage[1]", $("#ptext").val());
    data.append("UploadedImage[2]", $("#plink").val());

    // Add the uploaded image content to the form data collection
    var files = $("#puploader").get(0).files;
    if (files.length > 0) {
        data.append("UploadedImage[3]", files[0]);
    }

    // Make Ajax request with the contentType = false, and procesDate = false
    var ajaxRequest = $.ajax({
        type: "POST",
        url: "Service/Serve.asmx/SaveUpload",
        contentType: false,
        processData: false,
        data: data,
        success: function (response) {
            BindPostTop(response);
            $("#imgup").attr("src", "");
            $("#popimg").hide();
        },
        failure: function (response) {
            // alert(response.d);
        },
        error: function (response) {
            // alert(response.d);
        }
    });
});

以上是调用服务的ajax代码。它在localhost上工作得很棒,但在发布和测试时失败了。当我更改ajax中的内容类型时,它会成功,但不会上传。

0 个答案:

没有答案