const state = reactive({
name : 'aaa',
price : 1222,
imageUrl : require('../assets/logo.png'),
imageFile : null,
name1 : ['aaa1', 'aaa2'],
price1 : [1222, 1333],
imageUrl1 : [require('../assets/logo.png'),require('../assets/logo.png')],
imageFile1 : [new File([""],""), new File([""],"")],
});
const handleImage = (e) => {
if(e.target.files[0]){
state.imageUrl = URL.createObjectURL(e.target.files[0]);
state.imageFile = e.target.files[0];
}
else{
state.imageUrl = require('../assets/logo.png');
state.imageFile = null;
}
}
const handleImage1 = (e, idx) => {
if(e.target.files[0]){
state.imageUrl1[idx] = URL.createObjectURL(e.target.files[0]);
state.imageFile1[idx] = e.target.files[0];
}
else{
state.imageUrl1[idx] = require('../assets/logo.png');
state.imageFile1[idx] = new File([""],"");
}
}
const handleInsert = async() => {
const url =`/ROOT/api/product/insert.json`;
const headers = {"Content-Type":"form-data"};
const body = new FormData();
body.append("name", state.name);
body.append("price", state.price);
body.append("file", state.imageFile);
const response = await axios.post(url, body, {headers});
console.log(response.data);
}
const handleInsert1 = async() => {
const url =`/ROOT/api/product/insertbatch.json`;
const headers = {"Content-Type":"form-data"};
const body = new FormData();
console.log(state.name1.length);
for(let i=0;i<state.name1.length; i++){
body.append("name", state.name1[i]);
body.append("price", state.price1[i]);
body.append("file", state.imageFile1[i]);
}
const response = await axios.post(url, body, {headers});
console.log(response.data);
}
return {state,handleImage, handleInsert, handleImage1, handleInsert1}
}
파일에 빈 걸 넣어서 넘긴다.