zoukankan      html  css  js  c++  java
  • jquery uploadify修改上传的文件名和显示

    如果觉得看文章太麻烦,可以直接看参考:http://stackoverflow.com/questions/7707687/jquery-uploadify-change-file-name-as-it-is-being-uploaded

    贴上jquery uploadify官网地址:http://www.uploadify.com/

    哎呀,纠结了一上午,我说怎么file.name怎么都是我上传的名字,后来一查,我的天,原来是这个,data,我一直用的是file.name来显示的,才发现是用data来获取新的文件名称

    'onUploadSuccess' : function(file, data, response) {
            alert('Renamed file name is - ' + data);
        }

    我用的是thinkphp,所以修改的文件,麻烦哟,我的uploadify的文件路径是放在admin/Tpl/Index/uploadify这个路径下的,所以我的文件就变成了

    uploadify.php

    <?php
    /*
    Uploadify
    Copyright (c) 2012 Reactive Apps, Ronnie Garcia
    Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
    */
    
    // Define a destination
    //$targetFolder = 'stStone/Tpl/images/'; // Relative to the root
    
    $verifyToken = md5('unique_salt' . $_POST['timestamp']);
    
    if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
        $tempFile = $_FILES['Filedata']['tmp_name'];
       //由于是要前台显示,所以用了很多个dirname()
        $targetPath =  dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/Tpl/Index/images/';
    //获取文件的一些信息 $fileParts = pathinfo($_FILES['Filedata']['name']);
    //图片名称规则是 当前时间的时分秒.类型 如:20131221030820.jpg $filenames = date("YmdHis").".".$fileParts['extension'];
    //图片的完整路径咯
    $targetFile = rtrim($targetPath,'/') . '/' .$filenames; // Validate the file type $fileTypes = array('jpg','jpeg','gif','png'); // File extensions if (in_array($fileParts['extension'],$fileTypes)) { if(!file_exists($targetFile)){ move_uploaded_file($tempFile,$targetFile); chmod($targetFile,0777); }else{ unlink($targetFile); move_uploaded_file($tempFile,$targetFile); chmod($targetFile,0777); }
    //必须得返回这个图片名称哦
    echo $filenames; } else { echo 'Invalid file type.'; } } ?>

    页面调用方法:

    <?php $timestamp = strtotime(date("YmdHis")); ?>
        jQuery('#scroll_upload').uploadify({
            'multi':false,
            'formData'     : {
                    'timestamp' : '<?php echo $timestamp;?>',
                    'token'     : '<?php echo md5('unique_salt' . $timestamp);?>'
            },
            'swf':'<?php echo APP_PATH?>Tpl/Index/uploadify/uploadify.swf',
            'uploader': '<?php echo APP_PATH?>Tpl/Index/uploadify/uploadify.php',
             'buttonText':'上传图片',
             'auto': false,
             'multi': false,
            'onUploadSuccess' : function(file, data, response) {
              alert("新图片名字是"+data);
             }
        });    

    参数说明:

    #scroll_upload 是一个input type=file的按钮的id
    $timestamp 是当前时间的时间戳
    APP_PATH是当前项目的根目录,如后台使用,就是"./admin/"这样的(这里我也不是很懂啊,个人见解)
  • 相关阅读:
    (转+原)python中的浅拷贝和深拷贝
    (原)torch7中添加新的层
    (原+转)ubuntu终端输出彩色文字
    (原)torch中显示nn.Sequential()网络的详细情况
    (原)python中使用plt.show()时显示图像
    eclipse 注释模板
    leetcode 11 最大盛水容器
    leetcode 9 回文数字
    leetcode8 字符串转整数
    利用Intent启动activity的例子
  • 原文地址:https://www.cnblogs.com/fsong/p/3484936.html
Copyright © 2011-2022 走看看