-
-
jQuery.extend({
- createUploadIframe: function(id, uri)
- {
- //create frame
- var frameId = 'jUploadFrame' + id;
- var iframeHtml = '
过程:
(1 )前端文件的代码: test.php
相应的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 "<script>parent.stopSend('$name')</script>> ;";
- ?>
Copy code
Method three, original ajax file upload
|