作者: carlkyo 時間: 2019-1-4 14:52 標題: filereader javascript post
本帖最後由 carlkyo 於 2019-1-4 14:57 編輯
新年快樂
請問ching
我錯左邊啊
我post唔到select左既file
many thanks
- $(document).ready(function () {
- var fileList = [];
- if (window.File && window.FileList && window.FileReader) {
- $("#file").on("change", function(e) {
- var files = e.target.files,
- filesLength = files.length;
- for (var i = 0; i < filesLength; i++) {
- var f = files[i]
- var fileReader = new FileReader();
- fileReader.onload = (function(e) {
- var file = e.target;
- $("<span class="pip">" +
- "<img class="imageThumb" src="" + e.target.result + "" title="" + f.name + ""/>" +
- "<br/>"+f.name +
- "<br/><span class="remove">刪除</span>" +
- "</span>").insertAfter("#file");
- $(".remove").click(function(){
- $(this).parent(".pip").remove();
- });
- });
- fileReader.readAsDataURL(f);
- fileList.push(f);
- }
- console.log(fileList);
- });
- } else {
- alert("Your browser doesn't support to File API")
- }
- $('#form').on('submit', function (e) {
- e.preventDefault();
- $.ajax({
- url: 'post.php',
- type: 'POST',
- data: {name: fileList,request: 2},
- contentType: false,
- cache: false,
- processData: false,
- //dataType: 'json',
- success: function (response) {
- $('.jumbotron').html(response);
- },
- });
- });
- });
作者: CVSDF 時間: 2019-1-5 01:17
https://makitweb.com/how-to-uplo ... ng-ajax-and-jquery/
作者: carlkyo 時間: 2019-1-5 19:43
我想做到可以不停選文件圖片,最後先禁submit
好像上面張圖咁,選左之後唔啱可以刪除
thanks
作者: CVSDF 時間: 2019-1-6 02:56
Try use formdata object in ur ajax, seems works:
- var fd = new FormData();
- for(var x=0; x<$('#file')[0].files.length; x++){
- fd.append('file' + x , $('#file')[0].files[x] );
- }
-
- $.ajax({
- url: './post.php',
- type: 'POST',
- data : fd,
- contentType: false,
- cache: false,
- processData: false,
- success: function (response) {
- console.log(response);
- $('.jumbotron').html(response);
- },
- });
作者: CVSDF 時間: 2019-1-6 04:25
btw fileList好似係readonly的, 恐怕用code更改不了? :/
https://w3c.github.io/FileAPI/#dfn-filelist
Note: The HTMLInputElement interface has a readonly attribute of type FileList, which is what is being accessed in the above example. Other interfaces with a readonly attribute of type FileList include the DataTransfer interface.
作者: carlkyo 時間: 2019-1-6 11:25
many thanks
我再試試

