如何从ajax Post获取Base64?

时间:2018-10-15 22:45:25

标签: c# asp.net json ajax webmethod

我试图在我的web方法背后的代码中获取base64帖子, 但是似乎每当我包含base64时,我都会收到一个错误:服务器的响应状态为500(内部服务器错误)-它一直在执行错误功能。 如果未将base64包含在传递的数据中,则Post可与其他字符串一起使用。

function event_create() {
        alert("alert test : function works => onclick");

        function getBase64(file) {
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function () {
                console.log(reader.result);
            };
            reader.onerror = function (error) {
                console.log('Error: ', error);
            };
        }


        var eventTitle = $("#eventTitle").val();
        var eventDesc = $("#eventDesc").val();
        var eventTimeStart = $("#eventTimeStart").val();
        var eventTimeEnd = $("#eventTimeEnd").val();
        var eventDateStart = $("#eventDateStart").val();
        var eventDateEnd = $("#eventDateEnd").val();
        var eventType = $("#eventType").val();
        var eventPlace = $("#eventPlace").val();
        var eventAttendee = document.getElementById("lblSelected").innerText;
        var userID = sessionStorage.getItem("userID");
        var imageBase64 = getBase64(document.getElementById('test').files[0]);

        var data = { 'eventTitle': eventTitle, 'eventDesc': eventDesc, 'eventPlace': eventPlace, 'eventType': eventType, 'eventAttendee': eventAttendee, 'userID': userID, 'imageBase64': imageBase64};
        $.ajax({
            type: "POST",
            async: true,
            contentType: "application/json; charset=utf-8",
            url: ".../../../../Operation/insert.aspx/createEvent",
            data: JSON.stringify(data),
            datatype: "json",
            success: function (result) {

                if (result.d <= 0) {
                    //false alert something 
                    alert("FALSE");
                }
                else if (result.d > 0) {
                    //true
                    alert(result.d);
                }
                else {
                    alert("sent but no call-back");
                }
                console.log(result);
            },
            error: function (xmlhttprequest, textstatus, errorthrown) {
                alert(" connection to the server failed ");
                console.log("error: " + errorthrown);
            }
        });

    }

这是将获得帖子的Web方法

 [WebMethod(EnableSession = true)]
public static string createEvent(string eventTitle, string eventDesc, string eventPlace, string eventType, string eventAttendee, string userID, string imageBase64)
{
    String orgID = (String)HttpContext.Current.Session["orgID"];
    string response = orgID;


    string path = HttpContext.Current.Server.MapPath("~/Users/Organizer/organizerData/"); // de path


    //Check if directory exist
    if (!Directory.Exists(path))
    {
        Directory.CreateDirectory(path); //Create directory if it doesn't exist
    }


    string imageName = "event1" + ".jpg";// for instance

    //set the image path
    string imgPath = Path.Combine(path, imageName);

    byte[] imageBytes = Convert.FromBase64String(imageBase64);

    File.WriteAllBytes(imgPath, imageBytes); //write the file in the directory 


    return imageBase64;
}

0 个答案:

没有答案