doqrt26664 2016-10-15 19:03
浏览 95

React和Dropzone.js:以“插入模式”上传图片

I have a form to add user and outside it an upload field (dropzone.js) to manage the user's avatar image.

If the form is in edit mode, I know the user's id so I can manage the avatar; but If I want to add an user I Have no id to bind an image to new user.

Actually, I'm using this approach in insert mode:

  1. I upload the image on the server;
  2. Server return the filename;
  3. I add the filename in post request when the submit button is pressed to save user.

Others solutions could be:

  1. create an preview and calculate the image's base64 with dropzone and send it to server (but I don't figure out how implement this solution);
  2. in insert mode disable the upload field and enable it only in edit mode.

Are there better solutions?

EDIT 1: This is my code

var ModalAvatar = React.createClass({

    propTypes: {
        isActive        : React.PropTypes.bool.isRequired,
        onChange        : React.PropTypes.func.isRequired
    },

    componentDidMount: function() {

        var self = this;
        Dropzone.autoDiscover = false;
        this.dropzone = new Dropzone('#demo-upload', {
            parallelUploads: 1,
            thumbnailHeight: 120,
            thumbnailWidth: 120,
            maxFilesize: 3,
            filesizeBase: 1000,
        });

        this.dropzone.on("addedfile", function(file){

            // How I could read the added file by $_FILES?
            var url = URL.createObjectURL(file);
            var formData = new FormData();
            formData.append("avatarFile", file);
            self.props.onChange("avatarFile", formData);
        });

        // Now fake the file upload (I took this from dropzone.js)

        var minSteps = 6,
            maxSteps = 60,
            timeBetweenSteps = 100,
            bytesPerStep = 100000;

        this.dropzone.uploadFiles = function(files) {
            var self = this;

            for (var i = 0; i < files.length; i++) {

                var file = files[i];
                var totalSteps = Math.round(Math.min(maxSteps, Math.max(minSteps, file.size / bytesPerStep)));

                for (var step = 0; step < totalSteps; step++) {
                    var duration = timeBetweenSteps * (step + 1);
                    setTimeout(function(file, totalSteps, step) {
                        return function() {
                            file.upload = {

                                progress: 100 * (step + 1) / totalSteps,
                                total: file.size,
                                bytesSent: (step + 1) * file.size / totalSteps
                            };

                            self.emit('uploadprogress', file, file.upload.progress, file.upload.bytesSent);
                            if (file.upload.progress == 100) {
                                file.status = Dropzone.SUCCESS;
                                self.emit("success", file, 'success', null);
                                self.emit("complete", file);
                                self.processQueue();
                            }
                        };
                    }(file, totalSteps, step), duration);
                }
            }
        }




        this.toggle(this.props.isActive);
    },



    render: function(){
        return (
            <form action="/upload" className="dropzone needsclick dz-clickable" id="demo-upload">
                <div className="dz-message needsclick">
                    Drop files here or click to upload.
                </div>
            </form>
        )

    }
});



var User = React.createClass({

    getInitialState: function(){        
        return {
            isActive : false
        }
    }, 

    onChange: function(formData){
        this.setState({
            avatarImg: formData
        });
    },

    openModal: function(){
         this.setState({
             isActive: true
         });
    },
    render: function(){

        var model = {
            id              : this.state.id,
            avatarImg       : this.state.avatarImg
        };


        return (
            <div className="user">
                 <div className="user-avatar" onClick={this.openModal}><img src="path"></div>

                    <Form model={model} onSuccess={this.onSuccess}>
                        <FieldName value={name}  />
                        <FieldEmail value={email} />
                    </Form>

                    <ModalAvatar 
                         isActive={this.state.isActive} 
                         onChange={this.onChange} />
            </div>

        )

    }
});

In User component, I have a form to add name, email, etc and another div .user-avatar to show user's avatar and open modal to change it.

My idea is simulate to upload in dropzone, and with "addedfile" event transform file parameter in something that I can read in php with $_FILES, but with that code $_FILES is empty. What I wrong?

  • 写回答

1条回答 默认 最新

  • doujiao4705 2016-10-15 19:11
    关注

    I don't think you should allow the user upload image if he still haven't registered .. You should assign/upload images only when you have the user to do for.

    So this mean you don't approach to the server without the user itself or enough data to create the user. You can allow preview on the creation and you don't have to use base64. Read about it, "Javascript upload image". You can simply use URL.createObjectURL() or simply pass the file reference to a form data

    Unless you have a reason to backup the image as soon as the user select it. Don't use the server, what if I canceled the creatino of the user ? You have a picture none will use ? you will start using TTL and stuff like it ? Just. Be lazy when it comes to the server. For your good and the client. Use it when you really need it.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊