-
-
jQuery.extend({
- createUploadIframe: function(id, uri)
- {
- //create frame
- var frameId = 'jUploadFrame' + id;
- var iframeHtml = '
过程:
(1 )前端文件的代码: test.php
-
-
-
-
- function ajaxFileUpload()
- {
- $.ajaxFileUpload
- (
- {
- url:'doajaxfileupload.php', //你处理上传文件的服务端
- secureuri:false,
- fileElementId:'img',
- dataType: 'json',
- success: function (data)
- {
- alert(data.file_infor);
- }
- }
- )
- return false;
- }
-
-
复制代码
相应的HTML为:
这样客户端就完成了。
(2) 在服务器端的doajaxfileupload.php
此处为了简便的检测是否真正的传值过来了,你可以将它存起来了。
-
- $file_infor = var_export($_FILES,true);
- file_put_contents("d:file_infor.php".$file_infor);
-
复制代码
这样打来刚生成的file_infor.php文件时,又看到了熟悉的信息了:
-
- array(
- 'name'=>'lamp.jpg',
- 'type'=>'image/pjpeg',
- 'tmp_name'=>'c:windowstempphpFA.tmp',
- 'error'=>0,
- 'size'=>3127
- )
-
复制代码
Of course, the real processing is similar to this:
-
- $upFilePath = "d://";
- $ok=@move_uploaded_file($_FILES['img']['tmp_name'],$upFilePath);
- if($ok = == FALSE){
- echo json_encode('file_infor'=>'Upload failed');
- }else{
- echo json_encode('file_infor'=>'Upload successful');
- }
- ?>
Copy the code
Method 2, use the iframe framework to upload images
html code:
index.js file:
-
- $(function(){
- $("#upload_file").change(function(){
- $("#uploadFrom").submit();
- });
- });
- function stopSend(str){
- var im="";
- $("#msg").append(im);
- }
Copy code
upload.php file:
-
- $file=$_FILES['upfile'];
- $name=rand(0,500000).dechex(rand(0,10000)).".jpg";
- move_uploaded_file ($file['tmp_name'],"upload/images/".$name);
- //Call the js function of the iframe parent window
- echo "?>
Copy code
Method three, original ajax file upload
php代码:
-
- if(isset($_FILES["myfile"]))
- {
- $ret = array();
- $uploadDir = 'images'.DIRECTORY_SEPARATOR.date("Ymd").DIRECTORY_SEPARATOR;
- $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.$uploadDir;
- file_exists($dir) || (mkdir($dir,0777,true) && chmod($dir,0777));
- if(!is_array($_FILES["myfile"]["name"])) //single file
- {
- $fileName = time().uniqid().'.'.pathinfo($_FILES["myfile"]["name"])['extension'];
- move_uploaded_file($_FILES["myfile"]["tmp_name"],$dir.$fileName);
- $ret['file'] = DIRECTORY_SEPARATOR.$uploadDir.$fileName;
- }
- echo json_encode($ret);
- }
- ?>
复制代码
|