我想从直接目录上载文件。但在我的代码中,它将打开媒体库,然后选择要上载的文件
jQuery(\'#t_pdf\').on("click", function(){
// alert("got it");
var image = wp.media({
title: "Upload PDF for Assignments",
multiple: false
}).open().on("select",function(){
var uploaded_image = image.state().get("selection").first();
var getpdf = uploaded_image.toJSON().url;
jQuery("#show-pdf").html("<a class=\'btn btn-success\' target=\'_blank\' href=\'"+getpdf+"\'>View PDF</a>");
jQuery("#sub_file_pdf").val(getpdf);
});
});
当我单击按钮时
<input type="button" class="btn btn-info" id="t_pdf" value="Upload File" required>
<span id="show-pdf"></span>
<input type="hidden" id="sub_file_pdf" name="sub_file">
它将按照脚本打开媒体库,希望从direct files目录上载文件并将其上载到特定的自定义文件夹。
有人能帮我找到这个吗?
最合适的回答,由SO网友:Sabbir 整理而成
我用wordpress文件上载方法修复了此问题:
php file:
add_action( \'wp_ajax_file_upload\', \'file_upload_callbacks\' );
add_action( \'wp_ajax_nopriv_file_upload\', \'file_upload_callbacks\' );
function file_upload_callbacks() {
$arr_img_ext = array(\'application/pdf\');
if (in_array($_FILES[\'file\'][\'type\'], $arr_img_ext)) {
$upload = wp_upload_bits($_FILES["file"]["name"], null, file_get_contents($_FILES["file"]["tmp_name"]));
//$upload[\'url\'] will gives you uploaded file path
//var_dump($upload[\'url\']);
wp_send_json( $upload[\'url\'] );
}
wp_die();
}