开发者

Upload image from jquery modal popup

I want to upload an image with other fields that is presented in jquery modal popup. I've seen some answers here like, we can use iframe to upload image from jquery modal dialog, But i couln't understand cleary.. Can some one tell me how 开发者_JAVA技巧to do that?

Thanks!


Check out the jQuery Form Plugin plugin. It handles uploading files neatly.


I think Gidon's recommendation is a good one (+1) another alternative is the uploadify plugin, just to give you options.

Edited for file upload php

<?php


    $error = "";
    $msg = "";
    $filepath = "";
    $fileElementName = $_REQUEST['field'];  //input name

    if(!empty($_FILES[$fileElementName]['error']))
    {
        switch($_FILES[$fileElementName]['error'])
        {
            case '1':
                $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
                break;
            case '2':
                $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
                break;
            case '3':
                $error = 'The uploaded file was only partially uploaded';
                break;
            case '4':
                $error = 'No file was uploaded.';
                break;

            case '6':
                $error = 'Missing a temporary folder';
                break;
            case '7':
                $error = 'Failed to write file to disk';
                break;
            case '8':
                $error = 'File upload stopped by extension';
                break;
            case '999':
            default:
                $error = 'No error code avaiable';
        }
    }
    elseif(empty($_FILES[$fileElementName]['tmp_name']) || $_FILES[$fileElementName]['tmp_name'] == 'none')
    {
        $error = 'No file was uploaded..';
    }
    else 
    {
            $uploaddir = '../../images/content/';
            $uploadname = substr(basename($_FILES[$fileElementName]['name']), -4);
            $uploadfile = $uploaddir . $uploadname;

            if( move_uploaded_file($_FILES[$fileElementName]['tmp_name'], $uploadfile )) 
            {
                // Save image information to db or whatever you want to do here
                $addon_message = "File (" . $uploadfile . ") moved successfully.";
            } else {
                $addon_message = "File failed to copy.";
            }
            /* ----- */
            $msg .= " File Name: " . $_FILES[$fileElementName]['name'] . ", ";
            $msg .= " File Size: " . @filesize($_FILES[$fileElementName]['tmp_name']);
            $msg .= $addon_message;
            $filepath = substr($uploadfile, 3);
            //for security reason, we force to remove all uploaded file
            @unlink($_FILES[$fileElementName]);     
    }       
    echo "{";
    echo                "error: '" . $error . "',\n";
    echo                "msg: '" . $msg . "',\n";
    echo                "filepath: '" . $filepath . "'\n";
    echo "}";
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜