File Upload Problem

Hello All,
I’m Uploading files in custom folder in files.
It’s work perfectly on developer mode.

But doesn’t work on production mode.

In production mode all file are upload in files folder and most of tile file upload disconnect.
Please help me how can I fixed that.

Thanks

Share what you see in your JS console. Seems like your socketio is not setup correctly.

Hello @rmehta
I got the problem.
below is my upload script’s some part.

MyClass = frappe.ui.BaseList.extend({
//Have some methods in this area.
upload_multiple_files: function (files /*FileData array*/, args, opts) {
       var me = this;
       var i = -1;

       // upload the first file
       upload_next();
       // subsequent files will be uploaded after
       // upload_complete event is fired for the previous file
       $(document).on('upload_complete', on_upload);

       function upload_next() {
           i += 1;
           var file = files[i];
           args.is_private = 0;
           args.file_url = me.filter_list.get_filter('folder').value + '/' + (file.webkitRelativePath.length > 0? file.webkitRelativePath : file.name);
           args.folder = args.file_url.replace('/' + file.name, '');
           args.filename = file.name;
           delete args.file_url;
           frappe.upload.upload_file(file, args, opts);
           frappe.show_progress(__('Uploading'), i+1, files.length);
       }

       function on_upload(e, attachment) {
           if (i === files.length - 1) {
               $(document).off('upload_complete', on_upload);
               frappe.hide_progress();
               me.run();
               return;
           }
           upload_next();
       }
   }
})

I’m change the file_url and name.
Normally file name doesn’t contain folder name but I need with folder name.
So I set file name with folder name.
and I also replace write function.

def write_file(fname, content, content_type=None, is_private=0):
    """write file to disk with a random name (to compare)"""
    path_list=[]
    job_no = ""
    if frappe.form_dict.get('folder'):
        path_list = frappe.form_dict.folder.split('/')
        file_path = get_files_path(*path_list, is_private=is_private)
        job_no = frappe.form_dict.get('jon_no')
        if not job_no:
            if frappe.db.exists("Folder Manage", path_list[0]):
                first_folder = frappe.get_doc("Folder Manage", path_list[0])
                if first_folder.folder_type == "Designer":
                    job_no = path_list[2]
                else:
                    job_no = path_list[1]
        # create directory (if not exists)
        frappe.create_folder(file_path)
    else:
        file_path = get_files_path(is_private=is_private)
    # write the file
    with open(os.path.join(file_path.encode('utf-8'), fname.encode('utf-8')), 'w+') as f:
        f.write(content)

    path_list.append(fname)
    return {
        "file_url": get_files_path(*path_list, is_private=is_private).replace(get_files_path(is_private=is_private),
                                                                              '/files', 1),
        "folder": file_path,
        "job_no": job_no
    }

Problem is

when upload file by socketio
below funcrtion contain only file name.

But when I run server as development mode this time work perfectly, I think this time socketio.js doesn’t work or happend something.

socket.on('upload-accept-slice', (data) => {
		try {
			if (!socket.files[data.name]) {
				socket.files[data.name] = Object.assign({}, files_struct, data);
				socket.files[data.name].data = [];
			}

			//convert the ArrayBuffer to Buffer
			data.data = new Buffer(new Uint8Array(data.data));
			//save the data
			socket.files[data.name].data.push(data.data);
			socket.files[data.name].slice++;

			if (socket.files[data.name].slice * 100000 >= socket.files[data.name].size) {
				// do something with the data
				var fileBuffer = Buffer.concat(socket.files[data.name].data);

				const file_url = path.join((socket.files[data.name].is_private ? 'private' : 'public'),
					'files', data.name);
				const file_path = path.join('sites', get_site_name(socket), file_url);

				fs.writeFile(file_path, fileBuffer, (err) => {
					delete socket.files[data.name];
					if (err) return socket.emit('upload error');
					socket.emit('upload-end', {
						file_url: '/' + file_url
					});
				});
			} else {
				socket.emit('upload-request-slice', {
					currentSlice: socket.files[data.name].slice
				});
			}
		} catch (e) {
			console.log(e);
			socket.emit('upload-error', {
				error: e.message
			});
		}
	});
``
1 Like

Have you customized your file upload?

@rmehta
Yes
Page full js file
https://raw.githubusercontent.com/mainul94/image-man-service-pro/develop/image_processing_com/zenith/page/zfile_manager/zfile_manager.js

@mainul Very hard for me to debug that!

What does your file manager do? Reason why you must always contribute :wink:

Okey @rmehta
I explained you.
My uncle have a small company they re work for image manipulation like (Reove background, retouching etc.)
So there on only Photo or Image.
there workflow Receive a job from client → download images → distribute to Designer → QC → Upload ->Upload backup.
So there need ensure that folder structure return as like received.
same filename receive lot of times but file not same.

I have some solutions

1. I can solved by prev version of frappe

2. I can change socketio file but now time if I change socktio file then file upload in folder but socket disconnected. Hope if i study then I can solved.

! Important

I’m share this problem in this discussion because In frappe framework we have options to change or edit agrs but socketio doesn’t received our args. :wink:
So I thinks that is not fare for developers.

Thanks you so much @rmehta for your nice response.

1 Like