doubi6669 2015-12-13 19:41
浏览 107

AJAX请求php脚本

So the problem is: a user uplaods a file. I use е.PreventDefault(); to prevent the page from refreshing after the upload. Under the file upload form i've made a list of all the user's file names. Obviously the newly uploaded file name isn't in the list. After refreshing the page it gets in the list, but the business logic is that the file's name should go in the list withouth refreshing the page.

function showNewFileProperies() {
    var xhttp;           
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
           document.getElementById("results").innerHTML = xhttp.responseText;
        }
    };
    xhttp.open("GET", "../../app/Models/UserFiles.php" , true);
    xhttp.send();
}

This is the request with which I thought my problem would be solved.

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class UserFiles extends Model{
   protected $table = 'files';

   public function getUserFiles(){
       $getFiles = Filecontent::where('userid', Auth::id())->get();
       foreach($getFiles as $getFile) {
           $result = $getFile->filename;
           return $result;
       }
   }
}

I'm using Laravel and Dropzone.js. The js code i put in the success event of dropzone.js

  • 写回答

2条回答 默认 最新

  • doushang9172 2015-12-15 19:57
    关注

    Sorry for the delay in response. Here is what I used to achieve what you are describing. Sorry for the bad indenting but this editor is really bad.

    Dropzone.options.documentDropzone = {
    paramName: "file",
    dictDefaultMessage: "Drag your document over this box to upload or click here",
    maxFilesize: 100, // MB
    maxFiles: 10,
    
    addRemoveLinks: true,
    dictRemoveFile: 'Remove Attachment',
    accept: function(file, done) {
        done();
    },
    init: function() {
        this.on("addedfile", function(file) {
            busy_uploading = true;
        });
        this.on('removedfile', function(file){
            remove_file(file.previewElement, file.fileID);
            if(this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
                busy_uploading = false;
            }
        });
        this.on("complete", function (file) {
            if(this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
                busy_uploading = false;
            }
        });
    },
    success: function(file, response) {
        response = JSON && JSON.parse(response) || $.parseJSON(response);
        if(response.error) {
            var node, _i, _len, _ref, _results;
            var message = response.error;
            file.previewElement.classList.add("dz-error");
            _ref = file.previewElement.querySelectorAll("[data-dz-errormessage]");
            _results = [];
            for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                node = _ref[_i];
                _results.push(node.textContent = message);
            }
    
            return _results;
        } else {
            for(var i = 0; i < response.file_id.length; i++) {
                file.fileID = response.file_id[i];
                file.fileEXT = response.file_ext[i];
                add_file_id(response.file_id[i]);
                //You can add the uploaded item in your list here
            }
            return file.previewElement.classList.add("dz-success");
        }
    }
    };
    

    So this is roughly what I used. You can simply add the item on your list in the success function since that is where you will get the response from the server.

    I did not include the php because it is just your basic upload script. This will work with as many files as you wish.

    If you need more help just add a comment and I will try to assist you as best I can.

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)