简体   繁体   English

如何使用json Ajax将文件以及一些输入字符串发送到Codebehind中的webmethod?

[英]how to send file along with some input strings to a webmethod in Codebehind using json Ajax?

So I have this form that requires the user to fill-up some texts in inputbox, file(eg.docx,pdf etc) and Image (eg.jpg,png etc.), then all data will be send to [Webmethod] in codebehind -and do some process.. Ived successfully implemented the Strings from input box (eg.Title,Discription etc.) using json/ajax request.. and the only thing that got me stuck was the file to be passed through json and be recieved by codebehind.. Any help or suggestions will indeed be appreciated 所以我有这个表格,要求用户填写输入框,文件(例如docx,pdf等)和图像(例如jpg,png等)中的一些文本,然后所有数据都将发送到Ived成功地使用json / ajax请求从输入框中实现了字符串(例如,Title,Discription等)。而让我卡住的唯一一件事是文件要通过json传递并成为由代码隐藏接收。。任何帮助或建议将不胜感激

$.ajax({
    type: "POST",
    url: "insert.aspx/eventCreate",
    data: {
        'eventImage': eventImage,//here's the image
        'eventFile': eventFile, //here's the file
        'eventTitle': eventTitle,
        'eventDesc': eventDesc,
        'eventPlace': eventPlace,
        'eventType': eventType,
        'eventAttendee': eventAttendee,
        'userID': userID
    },
    async: true,
    contentType: "application/json; charset=utf-8",
    success: function (data, status) {
        console.log("Call successfull test");
        alert(data.d);
    },
    failure: function (data) {
        alert(data.d);
    },
    error: function (data) {
        alert(data.d);
    }
});

[WebMethod(EnableSession = true)]
public static string eventCreate(string eventTitle, string eventDesc, string eventPlace, string eventType, string eventAttendee, string UserID)
{
    //how do I get the Image and file from the request??


    return "0";
}

Sorry just noticed that you are using 'WebMethod'. 抱歉,您刚刚注意到您正在使用“ WebMethod”。

You could post your file as base64 string and receive the same in your WebMethod, and convert the base64 back to file in your WebMethod. 您可以将文件作为base64字符串发布,并在WebMethod中接收该字符串,然后将base64转换回WebMethod中的文件。

please follow the link how-to-convert-file-to-base64-in-javascript to convert file to base64. 请按照链接“ 如何在JavaScript中将文件转换为base64”将文件转换为base64。

and the link base64-encoded-string-to-file to convert base64 back to file in your webmethod. 和链接base64-encoded-string-to-file ,将base64转换回webmethod中的文件。

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);
   };
}

$.ajax({
    type: "POST",
    url: "insert.aspx/eventCreate",
    data: {
        'eventImage': eventImage,//here's the image
        'eventFile': eventFile, //here's the file
        'eventTitle': eventTitle,
        'eventDesc': eventDesc,
        'eventPlace': eventPlace,
        'eventType': eventType,
        'eventAttendee': eventAttendee,
        'userID': userID,
        'fileBase64': getBase64(document.getElementById('fileUploadId').files[0])
    },
    async: true,
    contentType: "application/json; charset=utf-8",
    success: function (data, status) {
        console.log("Call successfull test");
        alert(data.d);
    },
    failure: function (data) {
        alert(data.d);
    },
    error: function (data) {
        alert(data.d);
    }
});

[WebMethod(EnableSession = true)]
public static string eventCreate(string eventTitle, string eventDesc, string eventPlace, string eventType, string eventAttendee, string UserID, string fileBase64)
{
    //how do I get the Image and file from the request??
    File.WriteAllBytes(@"c:\yourfile", Convert.FromBase64String(fileBase64));


    return "0";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM