代码之家  ›  专栏  ›  技术社区  ›  Gowthaman

如何将文档从react native DocumentPicker response以byte[]的形式发送到web api

  •  0
  • Gowthaman  · 技术社区  · 7 年前

    我使用react native document picker表单从Android移动设备中选择文件,代码如下

    DocumentPicker.show({
      filetype: [DocumentPickerUtil.images()],
    },(error,res) => {
      // Android
      console.log(
         res.uri,
         res.type, // mime type
         res.fileName,
         res.fileSize
      );
    });
    

    uri: 'content://com.android.providers.media.documents/document/image%3A48',
    type: 'image/jpeg'
    fileName: 'TestImage.jpeg'
    fileSize: 5301
    

    如何在react native中获取上传文件的字节数组,并将其作为输入发送到webapi post请求。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Gowthaman    7 年前

    这对我有用

        DocumentPicker.show({
          filetype: [DocumentPickerUtil.images()],
        },(error,res) => {
     
      
            const data = new FormData();
            data.append('name', 'testName'); // you can append anyone.
            data.append('photo', {
              uri: res.uri,
              type: res.type,
              name: res.fileName,
            });
    
            fetch('http://ApiUrl/api/Controller/UploadFile', {
              method: 'post',
              headers: {
                'Content-Type': 'multipart/form-data',
              },
              body: data
            }).then(res => {
              console.log(res)
            })
            .catch(function(error) {
              console.log('There has been a problem with your fetch operation: ' + error.message);
                throw error;
              });
    });