簡體   English   中英

在不使用multipart的情況下將數據添加到jQuery文件上傳多路輸入

[英]Add data to jQuery file upload multi input without using multipart

我在用戶個人資料上有一個很大的形式,帶有一些文件輸入字段(3個或更多)。 所有字段的名稱都不同,后端驗證也不同。

我使用jQuery文件上傳。 多部分功能已關閉,因為表單具有許多其他字段,並且在上傳階段不需要來自它們的信息。 可以,但是我無法發送標記來標識某人的財產(合並表單和發送此表單的用戶)。

formData僅適用於多部分,在jQuery文件上傳的常見問題中,我看不到任何答案。

此代碼來自JS端

$('#fileupload').fileupload({
    limitConcurrentUploads: 5,
    maxNumberOfFiles: 1,
    multipart:false,
    url: 'fileApi.html',
    formData: {wav: true},
    add: function(e, data) {
                //many validation
                data.submit();

此代碼來自服務器端

public function post($print_response = true) {
    if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
        return $this->delete($print_response);
    }
    //если мультипарт выключен то массива файлес нет. есть только имя файла в заголовке запроса
    $upload = isset($_FILES[$this->options['param_name']]) ?
        $_FILES[$this->options['param_name']] : null;
    // Parse the Content-Disposition header, if available:
    $file_name = $this->get_server_var('HTTP_CONTENT_DISPOSITION') ?
        rawurldecode(preg_replace(
            '/(^[^"]+")|("$)/',
            '',
            $this->get_server_var('HTTP_CONTENT_DISPOSITION')
        )) : null;
    // Parse the Content-Range header, which has the following form:
    // Content-Range: bytes 0-524287/2000000
    $content_range = $this->get_server_var('HTTP_CONTENT_RANGE') ?
        preg_split('/[^0-9]+/', $this->get_server_var('HTTP_CONTENT_RANGE')) : null;
    $size =  $content_range ? $content_range[3] : null;
    $files = array();
    if ($upload && is_array($upload['tmp_name'])) {
        // param_name is an array identifier like "files[]",
        // $_FILES is a multi-dimensional array:
        foreach ($upload['tmp_name'] as $index => $value) {
            $files[] = $this->handle_file_upload(
                $upload['tmp_name'][$index],
                $file_name ? $file_name : $upload['name'][$index],
                $size ? $size : $upload['size'][$index],
                $upload['type'][$index],
                $upload['error'][$index],
                $index,
                $content_range,
                $_REQUEST['wav'][$index]
            );
        }
    } else {
        // param_name is a single object identifier like "file",
        // $_FILES is a one-dimensional array:
        $files[] = $this->handle_file_upload(
            isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
            $file_name ? $file_name : (isset($upload['name']) ? $upload['name'] : null),
            $size ? $size : (isset($upload['size']) ? $upload['size'] : $this->get_server_var('CONTENT_LENGTH')),
            isset($upload['type']) ? $upload['type'] : $this->get_server_var('CONTENT_TYPE'),
            isset($upload['error']) ? $upload['error'] : print_r($_REQUEST,true),//in this sting im debugg response
            null,
            $content_range,
            $_REQUEST['wav']
        );
    }

如何不帶多部分的方式發送附加參數?

在js中工作

    $('#fileupload').fileupload({
    limitConcurrentUploads: 5,
    maxNumberOfFiles: 1,
    multipart:true, 
    url: 'fileApi.html',
    add: function(e, data) {                
        data.formData = {files: data.files[0], wav: true} //files in php optins input name

在PHP中

    $this->options = array(
        ...
        'param_name' => 'files',

在這個

public function post($print_response = true) {
    if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
        return $this->delete($print_response);
    }
    //если мультипарт выключен то массива файлес нет. есть только имя файла в заголовке запроса
    $upload = isset($_FILES[$this->options['param_name']]) ?
        $_FILES[$this->options['param_name']] : null;

$ _FILES不為空

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM