2009-10-13 90 views
3

我想使用ajax上傳圖片。 在這個模塊:使用ajax和php上傳圖片

  1. 在點擊browse並選擇圖像,它將被上傳並在文件中的字段顯示。
  2. 按鈕添加標題和說明,並點擊後,該圖像將顯示在下方和上像場將是空白
+2

請問您可以發佈您當前的HTML/Javascript – 2009-10-13 08:16:03

+2

您嘗試了什麼,哪些方法無效以及錯誤代碼在哪裏?我非常抱歉不得不問,但我似乎錯過了我的水晶球.. – Duroth 2009-10-13 08:58:08

回答

3

您無法通過AJAX上傳文件。您需要使用IFRAME或基於Flash的上傳器。

+0

我寧願使用iframe,因爲我們不是建立閃存。 – mauris 2009-10-13 09:08:33

+0

...哪些是Ajax。 Ajax不是XHR。 XHR只是實現Ajax最常用的方式。 – Quentin 2009-10-13 09:13:07

+0

Firefox> = 3且Chrome> = 2支持XHR上傳。 – 2009-10-13 09:33:24

1

嘗試使用JQuery插件上傳圖片。

可能http://www.uploadify.com/

這會給一個想法如何做到這一點。

+0

即使那裏演示不工作在IE :-P – 2010-11-13 00:06:09

+0

@piemesons它在我的系統上使用IE 8的工作,請確保你已經安裝了Flash播放器。 – Bindas 2010-11-30 07:34:15

1

假設你在服務器端有一個句柄..這裏是一個小函數和關於如何在javascript中實現'iframe hack'的例子。

HTML

<form name="image-upload"> 
<input type="file" name="image" /></br> 
<button type="submit" name="upload">Upload</button> 
<div id="upload-results"></div> 
</form> 

的JavaScript

var fileUpload = function(form /* HTMLElement */, action /* Form Action URL */, callback /* Callback function */) { 
    /* vars */ 
    var atribs = { 
     "target": "upload_iframe", 
     "action": action, 
     "method": "post", 
     "enctype": "multipart/form-data", 
     "encoding": "multipart/form-data" 
    }, iframe; 
    /* iframe listener */ 
    var ilistener = function() { 
     var results; 
     listener.remove(this, 'load', ilistener); 
     if('contentDocument' in this) { 
      results = this.contentDocument.body.innerHTML; 
     } else if ('contentWindow' in this) { 
      results = this.contentWindow.document.body.innerHTML; 
     } else if ('document' in this) { 
      results = this.document.body.innerHTML; 
     } else { 
      throw "i'm dead jim :/"; 
     } 
     callback.apply(this,[results]); // call the callback, passing the results 
     this.parentNode.removeChild(this); // remove the iframe 
    }; 

    /* create the iframe */ 
    form.parentNode.appendChild(FragBuilder([{"tagName": "iframe","id": "upload_iframe","name": "upload_iframe","style": {"height": "0","width": "0","border": "0"}}])); 
    /* collect the iframe back */ 
    iframe = By.id('upload_iframe'); 
    /* set the form properties */ 
    for(var attr in atribs) { 
     if(attr in form) { 
      form[attr] = atribs[attr]; 
     } 
    } 
    /* attach the event listener to the iframe */ 
    listener.add(iframe, 'load', ilistener); 
    /* submitting the form */ 
    form.submit(); 
}; 

// get the form, and the results area 
var form = document.forms['image-upload'], results = By.id('upload-results'); 
// listen for the form submit, capture it, and run the iframe upload. 
listener.add(form, 'submit', function(e) { 
    e.preventDefault(); 
    results.innerHTML = 'Uploading...'; 
    fileUpload(this, 'server.php' /* really anything */, function(data) { 
     console.log(data); 
      results.innerHTML = "Uploaded!"; 
    }); 
}); 

請注意:爲了簡單起見我使用以下工具。 https://github.com/rlemon/FragBuilder.js來自JSON輸入的DocumentFragment構建器。
https://gist.github.com/2172100事件監聽器,和按實用功能。
*這些都很容易刪除。

+0

我意識到這是過去三年的事實......但我在尋找時遇到了這個問題。所以我認爲別人也可以。這裏是你2012年答案 – rlemon 2012-04-04 22:22:07

+0

我收到一個錯誤「ReferenceError:由未定義」「var form = document.forms ['image-upload'],results = By。id('upload-results');「 – Goldie 2012-11-20 19:31:59

+0

需要包含實用函數,請參閱代碼下面的註釋或將By.id更改爲document.getElementById – rlemon 2012-11-20 22:50:34

2

實際上,您可以在Jquery中使用ajax函數上傳圖像,至少使用lates版本的chrome。

HTML:

<form action="/" enctype="multipart/form-data" method="post" accept-charset="utf-8"> 
    <input type="file" name="image"/> 
    <button type="submit"> 
</form> 

JS:

$("form").submit(function(){ 
    var formData = new FormData($(this)[0]); 
    $.ajax({ 
     url: window.location.pathname, 
     type: 'POST', 
     data: formData, 
     async: false, 
     cache: false, 
     contentType: false, 
     processData: false, 
     success: function (data) { 
       alert(data); 
     } 
    }); 
    return false; 
}); 

該腳本將發送通過Ajax創建的文件數據到當前頁面POST請求。您可以通過更改url參數來明顯改變目的地。

+0

爲什麼要將表單元素包裝到jquery中,只能讀取它又是什麼?'(this)[0]'==='this' – Dogoku 2014-03-07 17:27:59