- <input type="file" id="file" /><br />
- <input type="button" id="upload" value="上传" />
script部分:
- $("#upload").click(function () {
- var formData = new FormData();
- formData.append("myfile", document.getElementById("file").files[0]);
- $.ajax({
- url: "index.php?r=chat/img",
- type: "POST",
- data: formData,
- /**
- *必须false才会自动加上正确的Content-Type
- */
- contentType: false,
- /**
- * 必须false才会避开jQuery对 formdata 的默认处理
- * XMLHttpRequest会对 formdata 进行正确的处理
- */
- processData: false,
- success: function (data) {
- if (data == true) {
- alert('上传成功');
- }
- },
- error: function () {
- alert("上传失败!");
- }
- });
- });
后台请求地址:
- public function actionImg(){
- $res = move_uploaded_file($_FILES['myfile']['tmp_name'],'./pic/'.$_FILES['myfile']['name']);
- echo $res;
- }