import React, { Component } from 'react'
import { StyleSheet, View, Text, Image } from 'react-native'
import { toast } from '../../utils/base';
export interface Props {

}

export interface State {

}

const getName = (str: string) => {
    let name = str.substring(str.lastIndexOf("/") + 1)
    return name
}

const uploadFile = (url: string, files: Array<string> | string, isMultiple?: boolean) => {
    console.log("files", files)
    let formData = new FormData();
    for (var i = 0; i < files.length; i++) {
        let name = getName(files[i].uri)
        console.log("name:", name)
        let file = { uri: files[i].uri, type: 'multipart/form-data', name: 'image.jpg' };   //这里的key(uri和type和name)不能改变,  
        formData.append("file", file);   //这里的files就是后台需要的key  
    }
    // let file = files[0].uri
    // formData.append("files", file);   //这里的files就是后台需要的key  

    // formData.append("files", file)

    // formData.append('file',{uri:'file://'+path,type:'multipart/form-data'})

    const option = {
        method: 'post',
        mode: 'cors',
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        body: formData
    };
    console.log("数据:", option.body)
    // formData.append('file', res);
    fetch(url, option).then(res1 =>
        res1.json()
    ).then((resj) => {
        console.log("json数据是:", resj)
        if (resj.code == 200) {
            // this.setState({
            //     id: resj.data.id,
            //     // progressBar: 1
            //     isUpload: true,
            // })
            // console.log("id0：", this.state.id)
            // this.progressBar()

        } else {
            // toast(resj.msg)
            // this.setState({ fileName: "请重新上传", progressBar: 0, isUpload: false })
        }

    }
    ).catch((err) => {
        console.log("错误是:", err)
        // alert(err)
        toast(err)
    })
}

export {
    uploadFile
}