inputType =“文件”通过javascript上传图片?

时间:2013-07-17 04:15:48

标签: c# javascript jquery html file-upload

我在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");
            }
        });

但我不知道如何为fileupload

执行此操作
  <input type='file' name='newProfilePic' accept='image/*'>

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

0 个答案:

没有答案