Knowledge Map

formData & file sending 본문

WEB/JAVASCRIPT

formData & file sending

2018. 2. 8. 14:16

form에 저장된 데이터들을 formData라는 것에 담을 수 있다. IE에서는 10이상 가능하다고 하지만 기본 기능만 가능하다.


참고 : 

https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData

http://blog.teamtreehouse.com/uploading-files-ajax


다만 이것으로 파일을 보낼때에는 setRequestHeader에서 Content-Type을 설정하게 되면 제대로 인식이 안되는 문제가 있는듯 하다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function ajaxPost(data, url) {
    console.log(name' => ', url);
    var promise = new Promise(function (resolve, reject) {
        var client = new XMLHttpRequest();
        var uri = url;
 
        client.open('POST', uri);
        //client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        //client.setRequestHeader("Content-Type", "multipart/form-data");
        //data = 'files=' + data.getAll('files')[0];
        client.send(data);
 
        client.onload = function () {
            if (this.status >= 200 && this.status < 300) {
                console.log(`${name======> done!!`);
                resolve(this.response);
            } else {
                reject(this.statusText);
            }
        }
    });
    return promise;
}
cs




'WEB > JAVASCRIPT' 카테고리의 다른 글

4x4 행렬곱 연산 테스트  (0) 2017.09.25
2차원 행렬 클래스  (0) 2017.09.08
Dispatch  (0) 2017.08.06
ES5 에서의 실행 컨텍스트  (0) 2017.07.31
javascript API Geolocation  (0) 2017.04.25
Comments