1

我需要将图像从移动设备上传到服务器。首先,我将图像转换为 base64 并将该数据作为 JSON 参数发送到 WCF 服务调用。但我的问题是代码总是进入 jQuery ajax 错误块。如果我减小 base64 编码图像的大小,它会显示成功消息。那么我做错了什么吗?

以下是我的 WCF 服务。[IMyNewApp.cs]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
using System.Web.UI.WebControls;

namespace MyApp
{

    [ServiceContract]
    public interface IMyNewApp
    {
          [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
         void Add(int ID ,  string PIC);

    }
}

[MyNewApp.svc.cs]

using System.Data;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web;
using System.ServiceModel.Web;

namespace MyApp
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MyNewApp: IMyNewApp
  {
      public void Add(int ID ,  string PIC)
            {
                    //my code
            }
   }
}
}

我的 jQueryCode:-

$("#useJSONP").click(function () {
                   var id=4;
                    var pic= "iVBORw0KGgoAAAANSUhEUgAAAC0AAAAtCAYAAAA6GuKaAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAABVVJREFUeNrsWVtPG1cQnt09voCNwTaCYkAEVTSJlEogFEr72h/Ql771r5WHSn2r+p5W6WPbhAAOIWkgTQ3EhkCw9+IL9nrt3c6ctRHY3sWAl/ghR7J827PnO3O+mW9mVlhZWYH5+fnPVVX9SVGURcMwmCAI0C/DsiwIBAJ6LBb7PRKJfL+wsGAK6+vrs5lMZj2bzUahz0cikdgbHx+/zzRN+5EAB4JBmJqaBp/f33dgy6enkEm/g8PDwzvBYPBXhrRY5ruYnIJQONyXFvajIQVRgL1UCk5OTr5luq5z0yJvesNB0wLTMvlnSZQAeuQeoZBtUMLbU6cjwIODgzA8HMEvALKqgF7RuZWuPwQQEa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HhGTI068YqFwoUlYxXCWR2fk7Vi7rL5pWX55veByTTO1wA3WxXg8/guPCOl3oCgyjxwUfxV0OHqnRxCUNn7MF6WpVd1+KoB4XwsbGxssm80m0+n0g36XbyzJTmdmZhZZMplEuYEv5+bmfkbw3+m6Huy3Z+NIiRoCfhmNRn9YXFx8878AAwBnfOKk/5TZXAAAAABJRU5ErkJggg==";
                    var userData = { "ID": id, "pic": pic};
                    $.ajax({
                        url: "http://myserver/MyApp.svc/Add?callback=?",
                        type: "POST",
                        data: userData,
                        crossDomain: true,
                        contentType: "application/bson; charset=utf-8",
                        dataType: "jsonp",
                        processdata: true,
                        success: function res(msg) {
                            alert("hello" );
                        },
                        error: function error(response) {
                            alert("error");
                            alert(response.responseText);
                            alert(JSON.stringify(response));
                        }
                    });

                  });

            });

请给我一个建议。谢谢。

4

1 回答 1

1

我发现了问题.. POST 和 jsonp 不兼容...所以它使用动词 GET,但它有大小限制..这就是为什么大图像不上传...

所以我使用 CORS 和 POST 方法实现了跨域调用,现在我可以将图像上传到服务器......

谢谢..

于 2013-07-12T10:36:24.913 回答