【问题标题】:Expo Download file. FileSystem Download Async method POST with body世博会下载文件。 FileSystem 下载带有正文的异步方法 POST
【发布时间】:2021-07-20 03:16:10
【问题描述】:

我需要一种方法来通过传递正文的方法 Post 发出请求,但我没有找到方法。文档:https://docs.expo.io/versions/latest/sdk/filesystem/ 仅显示 GET 方法,我需要一种方法来发出通过正文的发布请求。

FileSystem.downloadAsync(${baseUrl}/v1/paycheck/pdf, FileSystem.documentDirectory + ‘file.pdf’,
{
headers: {
‘Authorization’: localToken
},
httpMethod: ‘POST’,

            body: {
                type: 'monthy',
                year: '2021',
                month: 2,
                employer: {
                    name: "Pink",
                }
            }
        }
    )
        .then(({uri}) => {
            Sharing.shareAsync(uri, {dialogTitle: 'Salvar ou Compartilhar'})

        })
        .catch(error => {
            console.error(error);
        });
}

【问题讨论】:

  • 为什么不先执行POST 请求以获取uri 然后下载它。??
  • @KartikeyVaish 它们是敏感数据,不能根据与客户端的合同存储在服务器上。
  • 但是您正在执行POST 请求。这就是我要说的...通过添加正确的Headers 来执行POST 请求,获取响应(uri)和uri 的共享......
  • @KartikeyVaish 出于某种原因 JSON.stringify 破坏了 Json 并导致 Json 出现错误。我使它与 axios 一起工作。非常感谢您的帮助。

标签: reactjs react-native expo native downloadfile


【解决方案1】:

据我了解您的问题

我对@9​​87654321@ 和Sharing PDF 的方法是

编写这两个函数

// Execute this function when you to share the file...
const GetPDF = async () => {
  try {
    const response = await fetch(`${baseUrl}/v1/paycheck/pdf`, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: "localToken",
      },
      body: JSON.stringify({
        type: "monthy",
        year: "2021",
        month: 2,
        employer: {
          name: "Pink",
        },
      }),
    });

    const content = await response.json();
    DownloadThenShare(content); // Some URI
  } catch (error) {
    console.error(error);
  }
};

现在DownloadAndShare函数

// This function will execute after Download has been completed successfully
const DownloadThenShare = async (uri) => {
  const downloadInstance = FileSystem.createDownloadResumable(
    uri,
    FileSystem.documentDirectory + "file.pdf"
  );

  const result = await FileSystem.downloadInstance.downloadAsync();

  if (result.status === 200) {
    Sharing.shareAsync(result.uri, { dialogTitle: "Salvar ou Compartilhar" });
  } else {
    console.log("Failed to Download");
  }
};

【讨论】:

    【解决方案2】:

    我终于设法使用 axios e FileReader();

       const response = await axios.post(`${baseUrl}/v1/paycheck/pdf`, data, {responseType: 'blob'});
            const fr = new FileReader();
            fr.onload = async () => {
                const fileUri = `${FileSystem.documentDirectory}/document.pdf`;
                const result = await FileSystem.writeAsStringAsync(fileUri, fr.result.split(',')[1], {encoding: FileSystem.EncodingType.Base64});
                saveFile(fileUri);
    
            };
            fr.readAsDataURL(response.data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多