admin管理员组文章数量:1336281
Cannot for the life of me figure out why the blob isn't uploading. I'm getting everything except the file. The file just isn't appended.
i can get it in if i base64 encode the blob, but then its causing problems loading it as a file.
for reference: apiClient is just a custom wrapper around fetch.
Client Side:
export async function uploadToServer(apiClient: AGFarmsClient, uri: string, filePath: string, bucket: string, mimeType: string) {
console.log("Starting file upload...");
console.log("URI: ", uri);
// Fetch the file from the provided URI
const r = await fetch(uri);
const file = await r.blob();
try {
const formData = new FormData();
formData.append('file_name', filePath);
formData.append('bucket', bucket);
formData.append('mime_type', mimeType);
formData.append('file', file);
// Step 4: Make the POST request to upload the file
const uploadResponse = await apiClient.post("/resources/media", formData, true);
console.log("Upload response:", uploadResponse);
return uploadResponse;
} catch (error) {
console.error("Error during file upload: ", error);
throw error;
}
}
async post<T>(url: string, data?: any, isFormData: boolean = false, customHeaders?: Record<string, string>): Promise<AGFarmsResponse<T>> {
if (isFormData) {
this.headers.delete('Content-Type'); // Remove any Content-Type set
} else {
this.headers.set('Content-Type', 'application/json');
}
const body = isFormData ? data : JSON.stringify(data);
return this.request<T>(url, 'POST', body, customHeaders);
}
private async request<T>(
url: string,
method: string,
body?: any,
customHeaders?: Record<string, string>
): Promise<AGFarmsResponse<T>> {
const requestUrl = new URL(url, this.baseURL);
const requestOptions: RequestInit = {
method,
headers: new Headers(this.headers), // Start with the default headers
};
// Add custom headers if provided
if (customHeaders) {
const headers = new Headers(requestOptions.headers);
Object.entries(customHeaders).forEach(([key, value]) => {
headers.set(key, value);
});
requestOptions.headers = headers;
}
try {
const response = await fetch(requestUrl.toString(), requestOptions);
const responseData = await response.json();
return this.normalizeResponse<T>(response, responseData);
} catch (error) {
return this.normalizeError(error as Error, url, method);
}
}
本文标签: typescriptupload function not working on reactnative expoStack Overflow
版权声明:本文标题:typescript - upload function not working on react-native expo - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742405530a2468741.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论