ajax를 사용하여 이미지를 업로드하고 싶습니다. 이 모듈은 : browse
이미지를 클릭 선택에ajax 및 php로 이미지 업로드
- 이를 업로드 파일 필드를 통해 표시한다. 버튼을 제목과 설명, 그리고 클릭을 추가 한 후
- 는, 그 이미지는 아래에 표시되고 상단 이미지 필드 당신은 AJAX를 통해 파일을 업로드 할 수 없습니다
ajax를 사용하여 이미지를 업로드하고 싶습니다. 이 모듈은 : browse
이미지를 클릭 선택에ajax 및 php로 이미지 업로드
이미지를 업로드 할 때 JQuery 플러그인을 사용해보세요.
는이 http://www.uploadify.com/ 그것을 수행하는 방법 아이디어를 줄 것이다 될 수 있습니다.
데모가 IE에서 작동하지 않습니다 : - P –
@piemesons IE 8을 사용하는 시스템에서 작동합니다. 플래시 플레이어가 설치되어 있는지 확인하십시오. – Bindas
서버 측에서 처리한다고 가정하면 여기에 작은 함수와 자바 스크립트에서 'iframe 해킹'을 구현하는 방법에 대한 예제가 있습니다.
HTML
<form name="image-upload">
<input type="file" name="image" /></br>
<button type="submit" name="upload">Upload</button>
<div id="upload-results"></div>
</form>
자바 스크립트
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 이벤트 리스너 및 유틸리티 별 기능
*이 둘은 쉽게 제거됩니다.
나는 이것이 사실을 지난 3 년이라는 것을 깨닫는다.. 그러나 나는 검색하는 동안 이것을 보았다. 그래서 나는 다른 사람들도 마찬가지라고 생각한다. 여기에 2012 답변이 있습니다 – rlemon
"ReferenceError : By 정의되지 않았습니다" "var form = document.forms [ 'image-upload'], results = By.id ('upload-results'); " – Goldie
유틸리티 함수도 포함해야합니다. 코드 아래의 참고 사항을 참조하거나 By.id를 document.getElementById로 변경하십시오. – rlemon
사실 jQuery의 ajax 함수를 사용하여 이미지를 업로드 할 수 있습니다.
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 매개 변수를 변경하여 대상을 분명히 변경할 수 있습니다.
jquery에서 양식 요소를 래핑하는 이유는 무엇입니까? 다시하겠습니까?'$ (this) [0]'==='this' – Dogoku
현재 HTML/자바 스크립트를 올리시겠습니까 –
무엇을 시도 했습니까? 어떤 코드가 작동하지 않았고 오류 코드는 어디에 있습니까? 물어볼 필요가있어서 정말 유감이지만, 내 수정 구슬을 잘못 놓은 것 같습니다. – Duroth