原生js上传文件


js核心代码:

function UploadFile() {
    var formData = new FormData();
    jQuery.support.cors = true;
   var file = $("#uploadFile")[0].files[0];
   formData.append("file", file);
   $.ajax({
   type: "post",
   url: "../Service/PicFile.ashx",
   data: formData,
   async: false,
   contentType: false,
   processData: false,
   success: function (data) {
   $("#hdnpic").val(data);

},
error: function (data) {
   console.log(data.statusText);
   }
   });
}

 

function btnAdd() {
    UploadFile();
    $("#btnSure").click();
}

<script type="text/javascript">
function readLocalFile() {

var localFile = document.getElementById("uploadFile").files[0];

var reader = new FileReader();

var content;
reader.onload = function (event) {
content = event.target.result;
document.getElementById("img").src = content;

}
reader.onerror = function (event) {
alert('error')
alert("File could not be read! Code " + event.target.error.code);
}
content = reader.readAsDataURL(localFile, "UTF-8");
}

</script>

 

 

 

asp页面

//文件上传

<tr>
    <td class="TblHead" style="width: 30%;">

    <a href="javascript:;" class="file">选择照片<input type="file" id="uploadFile" accept="image/*" onchange="readLocalFile();" /></a>

</td>
<td>
    <div id="picdiv" style="overflow: auto;">
    <img src="" id="img" style="border: 0px solid black;" alt="" runat="server" />
</div>
     <asp:HiddenField ID="hdnpic" runat="server" />
</td>
</tr>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM