<script> function submitFun(){ //逻辑判断 return true; //允许表单提交 //逻辑判断 return false;//不允许表单提交 } </script> <form onsubmit="reture submitFun();"> //注意此处不能写成 onsubmit="submitFun();"否则将表单总是提交 </form>
每日归档: 2015年3月31日
图片ajax上传
用的是CI框架
后端代码:
<?php class Upload extends MY_Controller { function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); } function index() { $data = array(); $data['upload_url'] = site_url('upload/ajax_upload'); $this->load->view('upload', $data); } function do_upload() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '1024'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $this->load->library('upload', $config); if (!$this->upload->do_upload('AidImg')) { echo("<script>parent.callback('" . $this->upload->display_errors() . "',false)</script>"); exit; } else { $data = array('upload_data' => $this->upload->data()); $img_path = base_url() . 'uploads/'; echo "<script>parent.callback('" . $data['upload_data']['file_name'] . "',true,'" . $img_path . "')</script>"; exit; } } } ?>
前端代码:
<html> <head> <title>Upload Form</title> <script type="text/javascript" src="<?php echo base_url() ?>access/js/jquery-1.8.0.min.js"></script> </head> <body> <form id="formImg" action="do_upload" method="post" target="hidden_frame" enctype="multipart/form-data"> <div> <input type="hidden" name="sh_id" id="sh_id" value="{$id}"> <!--这里的{$id}大家可以随便填1个进去--> <input id="AidImg" type="file" name="AidImg" onchange="uploadImg()"/> <div style="display:none;" id="imgError">图片不可为空</div> <iframe style="display:none" name='hidden_frame' id="hidden_frame"></iframe> <div><img id="p_img" src="" width="80" height="80"/> </div> <span class="help_inline">尺寸:80*80</span> </div> </form> </body> </html> <script> function uploadImg() { var names = $("#AidImg").val().split("."); if (names[1] != "gif" && names[1] != "GIF" && names[1] != "jpg" && names[1] != "JPG" && names[1] != "png" && names[1] != "PNG") { $("#imgError").html("<span>" + "图片必须为gif,jpg,png格式" + "</span>"); $("#formImg .help-inline").hide(); $("#imgError").show(); return; } $("#formImg").submit(); $("#imgError").show(); $("#imgError").html("图片上传中ing"); } function callback(message, success, path) { if (success == false) { $("#imgError").html("<span>" + message + "</span>"); $("#imgError").show(); } else { $("#imgError").hide(); $(".fromtrs").show(); $("#formImg .help-inline").hide(); var paths = path; $("#p_img").attr("src", path + message); $("#p_img").attr("imgname", message); //这里由于数据库里只存入图片名称加后缀名,故和路径拆开了 } } </script>
Using Form-based File Upload in Snoopy
Snoopy is capable of handling form-based file uploads according to RFC's 1867
and 2388. This note describes how to use Snoopy in a number of different file
upload scenarios. Snoopy will handle multiple file upload fields per request
and multiple files per field. Snoopy can also suggest a different filename in
the upload request from the "real" local filename. Snoopy can also put
Content-type into the file upload request on a per file basis.
EXAMPLE 1: SIMPLE FILE UPLOAD
In this example there is a single field with a single file to be uploaded and
nothing fancy. Note that it is necessary to call the set_submit_multipart()
function for the file upload to work. The filename suggested to the server
will be "myfile.txt" since the path will be removed.
include "Snoopy.class.inc";
$snoopy = new Snoopy;
$upload_url = "http://www.somedomain/upload.cgi";
$upload_vars = array();
$upload_files["FIELD1"] = "/home/me/myfile.txt";
$snoopy->set_submit_multipart();
if($snoopy->submit($upload_url, $upload_vars, $upload_files))
echo "<PRE>".$snoopy->results."</PRE>\n";
else
echo "error with file upload: ".$snoopy->error."\n";
EXAMPLE 2: MULTIPLE FILES IN A SINGLE FIELD
Snoopy will support sending multiple files in response to a single form field.
This example works when sending multiple files to an Apache/PHP server. Other
servers may not support this in quite the same way.
include "Snoopy.class.inc";
$snoopy = new Snoopy;
$upload_url = "http://www.somedomain/upload.php";
$upload_vars = array();
$upload_files["FIELD1[]"] = array("/home/me/myfile1.txt", "/home/me/myfile2.txt");
$snoopy->set_submit_multipart();
if($snoopy->submit($upload_url, $upload_vars, $upload_files))
echo "<PRE>".$snoopy->results."</PRE>\n";
else
echo "error with file upload: ".$snoopy->error."\n";
EXAMPLE 3: SPECIFYING FILENAMES and CONTENT TYPES
Some hosts are fussy about the filename that is passed to them by the file
upload request. We can overcome this by getting Snoopy to suggest a different
filename in the request than the "real" local filename. We can also get
Snoopy to specify an optional content type for each file. This is achieved by
having each file entry be an array of parameters instead of a simple filename.
include "Snoopy.class.inc";
$snoopy = new Snoopy;
$upload_url = "http://www.somedomain/upload.cgi";
$upload_vars = array();
$upload_files["FIELD1[]"] = array(
array("name" => "/home/me/myfile1.txt",
"remotename" => "C:\UPLOAD.TXT",
"type" => "text/plain"),
array("name" => "/home/me/myfile2.tiff",
"remotename" => "C:\UPLOAD.TIF",
"type" => "image/tiff")
);
$snoopy->set_submit_multipart();
if($snoopy->submit($upload_url, $upload_vars, $upload_files))
echo "<PRE>".$snoopy->results."</PRE>\n";
else
echo "error with file upload: ".$snoopy->error."\n";