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
});
}
});
``