admin管理员组

文章数量:1316204

I want to upload files from direct directory. But in my codes it will open media library then select files to upload

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

    });

});

When I click button

<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">

it will follow the script and open media library, want to upload files from direct files directory and upload it to specific custom folder.

Can anyone help me to find out this?

I want to upload files from direct directory. But in my codes it will open media library then select files to upload

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

    });

});

When I click button

<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">

it will follow the script and open media library, want to upload files from direct files directory and upload it to specific custom folder.

Can anyone help me to find out this?

Share Improve this question asked Oct 19, 2020 at 17:32 ShawnShawn 11710 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I fix this problems with wordpress file upload method:

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

本文标签: jquerywordpress file upload from direct directory not working