Working Example
是,當用戶添加文件,使用http://api.imgur.com/examples:
function upload(file) {
// file is from a <input> tag or from Drag'n Drop
// Is the file an image?
if (!file || !file.type.match(/image.*/)) return;
// It is!
// Let's build a FormData object
var fd = new FormData();
fd.append("image", file); // Append the file
fd.append("key", "6528448c258cff474ca9701c5bab6927");
// Get your own key: http://api.imgur.com/
// Create the XHR (Cross-Domain XHR FTW!!!)
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://api.imgur.com/2/upload.json"); // Boooom!
xhr.onload = function() {
// Big win!
// The URL of the image is:
document.getElementById('TextAreaId').innerHTML = JSON.parse(xhr.responseText).upload.links.imgur_page;
}
// Ok, I don't handle the errors. An exercice for the reader.
// And now, we send the formdata
xhr.send(fd);
}
來源
2011-08-25 20:23:24
Joe
你爲什麼要允許用戶下載他們剛剛上傳的文件。我可以理解上傳/存儲供以後使用,但現在上傳並提供可能有點多餘。 –
@Marc B:你的意思是像imageshack和相關? – Konerak
這是因爲當我在我的公司製作票時,我喜歡通過屏幕截圖或文件ID ......我們不允許在此過程中使用第三方程序或網站。 – Bulvak