0

我在我的 ASP.NET 页面中上传图像文件时遇到问题。

我的表单是动态生成的,我使用 ajax/jquery 将数据发送到我的代码后面

我的表格:

    return "<div class='item'>" +
        "<h3>Create Account For New Staff</h3>" +
        "<p>Fill in staff's particulars in the form below</p>" +
        "<div style='width: 70%;'>" +
        "<table class='style1'><tr>" +
        "<td class='style2'>NRIC:</td><td><input required type='text' id='newNRIC' value='' placeholder='NRIC' /></td></tr><tr>" +
        "<td class='style2'>Name:</td><td><input required type='text' id='newName' value='' placeholder='Name' /></td></tr><tr>" +
        "<td class='style2'>Gender:</td><td><select><option value='male'>Male</option><option value='Female'>Female</option></select></td></tr><tr>" +
        "<td class='style2'>DOB:</td><td><input required type='date' name='DOB' /></td></tr><tr>" +
        "<td class='style2'>Address:</td><td><input required type='text' id='newAddress' value='' placeholder='Address' /></td></tr><tr>" +
        "<td class='style2'>Email:</td><td><input required type='text' id='newEmail' value='' placeholder='Email' /></td></tr><tr>" +
        "<td class='style2'>ContactNo:</td><td><input required type='text' id='newContact' value='' placeholder='Contact Number' /></td></tr><tr>" +
        "<td class='style2'>Image:</td><td><input type='file' name='newProfilePic' accept='image/*'></td></tr><tr><td class='style2'>&nbsp;</td>" +
        "<td><button class='btn' type='button' onclick='addStaffModalSubmit()'>Submit</button></td></tr></table></div></div>"

以及要上传的代码:

        var newNRIC = document.getElementById('newNRIC').value;
        var newName = document.getElementById('newName').value;
        var newGender = document.getElementById('newGender').value;
        var newDOB = document.getElementById('newDOB').value;
        var newAddress = document.getElementById('newAddress').value;
        var newEmail = document.getElementById('newEmail').value;
        var newContact = document.getElementById('newContact').value;
        $.ajax({
            type: "POST",
            url: "Default.aspx/addStaff",
            data: {
                newNRIC: newNRIC,
                newName: newName,
                newGender: newGender,
                newDOB: newDOB,
                newAddress: newAddress,
                newEmail: newEmail,
                newContact: newContact
            },
            contentType: "application/json",
            dataType: "json",
            success: function (msg) {
                alert("Successfully added staff");
            }
        });

但我不知道如何为文件上传执行此操作

  <input type='file' name='newProfilePic' accept='image/*'>

我正在考虑通过javascript上传它并将文件路径发送到我后面的c#代码。任何人都知道如何做到这一点?

4

0 回答 0