我无法在名称中附加带空格的文件,但是没有空格时 成功附加,我正在使用codeigniter, 在附加文件之前将文件上传到服务器,我使用辅助路径获取到我文件的路径。
function upload_file() {
//$this->load->helper('path');
//$path = set_realpath('./uploads/');
//upload file
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$config['max_filename'] = '255';
$config['encrypt_name'] = FALSE;
$config['max_size'] = '50000';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if (isset($_FILES['file']['name'])) {
if (0 < $_FILES['file']['error']) {
echo 'Error durante la carga' . $_FILES['file']['error'];
} else {
if (file_exists('./uploads/' . $_FILES['file']['name'])) {
echo 'Nombre de archivo ya existe : uploads/' . $_FILES['file']['name'];
} else {
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file')) {
echo $this->upload->display_errors();
} else {
echo 'Archivo cargado! : ./uploads/' . $_FILES['file']['name'];
}
}
}
} else {
echo 'Porfavor selecciona un archivo';
}
}
sendMail函数的一部分:
$archivo = $_FILES['file']['name'];
$path = set_realpath('./uploads/');
$adjunto = $path.$archivo;
$this->email->attach($adjunto);
部分视图和JS
<input type="file" id="file" name="file" />
$(document).ready(function (e) {
$('#file').on('change', function () {
var file_data = $('#file').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: 'http://mail.mayordomus.cl/Cpersona/upload_file',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function (response) {
$('#msg').html(response);
},
error: function (response) {
$('#msg').html(response);
}
});
});
});
答案 0 :(得分:0)
您可以使用File
构造函数
let files = [new File([1], "file name.txt"), new File([2], "file name1.txt")];
let fd = new FormData();
for (let i = 0; i < files.length; i++) {
console.log(`Original file name: ${files[i].name}`);
let filename = files[i].name.replace(/\s/g, "-");
let file = new File([files[i].slice()], filename);
fd.append("files-" + i, file);
}
for (let [, prop] of fd) {
console.log(`FormData file name:${prop.name}`);
}
&#13;