int main() { emscripten_fetch_attr_t attr; emscripten_fetch_attr_init(&attr); attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_WAITABLE; emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png"); assert(fetch != 0); memset(&attr, 0, sizeof(attr)); // emscripten_fetch() must be able to operate without referencing to this structure after the call. printf("Main thread waiting for fetch to finish...\n"); emscripten_fetch_wait(fetch, INFINITY); printf("Main thread waiting for fetch to finish done...\n"); assert(fetch->data != 0); assert(fetch->numBytes > 0); assert(fetch->totalBytes == fetch->numBytes); assert(fetch->readyState == 4/*DONE*/); assert(fetch->status == 200); uint8_t checksum = 0; for(int i = 0; i < fetch->numBytes; ++i) checksum ^= fetch->data[i]; printf("Data checksum: %02X\n", checksum); assert(checksum == 0x08); emscripten_fetch_close(fetch); #ifdef REPORT_RESULT REPORT_RESULT(0); #endif }
int main() { // 1. Populate file to IndexedDB by a GET. emscripten_fetch_attr_t attr; emscripten_fetch_attr_init(&attr); strcpy(attr.requestMethod, "GET"); attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_PERSIST_FILE; emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png"); assert(fetch->status == 200 && "Initial XHR GET of gears.png should have succeeded"); emscripten_fetch_close(fetch); // 2. Make sure it is there. emscripten_fetch_attr_init(&attr); strcpy(attr.requestMethod, "GET"); attr.attributes = EMSCRIPTEN_FETCH_NO_DOWNLOAD | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; fetch = emscripten_fetch(&attr, "gears.png"); assert(fetch->status == 200 && "emscripten_fetch from IndexedDB should have found the file"); assert(fetch->numBytes > 0); assert(fetch->data != 0); emscripten_fetch_close(fetch); // 3. Delete the file from IndexedDB by invoking an Emscripten-specific request "EM_IDB_DELETE". emscripten_fetch_attr_init(&attr); strcpy(attr.requestMethod, "EM_IDB_DELETE"); attr.attributes = EMSCRIPTEN_FETCH_SYNCHRONOUS; fetch = emscripten_fetch(&attr, "gears.png"); assert(fetch->status == 200 && "Deleting the file from IndexedDB should have succeeded"); assert(fetch->numBytes == 0); assert(fetch->data == 0); emscripten_fetch_close(fetch); // 4. Now try to get the file again from IndexedDB, and ensure it is no longer available. emscripten_fetch_attr_init(&attr); strcpy(attr.requestMethod, "GET"); attr.attributes = EMSCRIPTEN_FETCH_NO_DOWNLOAD | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; fetch = emscripten_fetch(&attr, "gears.png"); assert(fetch->status == 404 && "Attempting to GET the file from IndexedDB again should have failed after the file has been deleted"); assert(fetch->numBytes == 0); assert(fetch->data == 0); emscripten_fetch_close(fetch); printf("Test succeeded!\n"); #ifdef REPORT_RESULT REPORT_RESULT(0); #endif }
int main() { emscripten_fetch_attr_t attr; emscripten_fetch_attr_init(&attr); strcpy(attr.requestMethod, "GET"); attr.userData = (void*)0x12345678; attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_PERSIST_FILE; attr.onsuccess = [](emscripten_fetch_t *fetch) { assert(fetch); printf("Finished downloading %llu bytes\n", fetch->totalBytes); assert(fetch->url); assert(!strcmp(fetch->url, "gears.png")); assert(fetch->id != 0); assert((uintptr_t)fetch->userData == 0x12345678); assert(fetch->totalBytes == 6407); emscripten_fetch_close(fetch); #ifdef REPORT_RESULT result = 1; REPORT_RESULT(); #endif }; attr.onprogress = [](emscripten_fetch_t *fetch) { assert(fetch); if (fetch->status != 200) return; if (fetch->totalBytes > 0) { printf("Downloading.. %.2f%% complete.\n", (fetch->dataOffset + fetch->numBytes) * 100.0 / fetch->totalBytes); } else { printf("Downloading.. %lld bytes complete.\n", fetch->dataOffset + fetch->numBytes); } }; attr.onerror = [](emscripten_fetch_t *fetch) { printf("Download failed!\n"); }; emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png"); assert(fetch != 0); memset(&attr, 0, sizeof(attr)); // emscripten_fetch() must be able to operate without referencing to this structure after the call. }
void success(emscripten_fetch_t *fetch) { printf("Deleting file from IDB succeeded.\n"); emscripten_fetch_close(fetch); }
void failure(emscripten_fetch_t *fetch) { printf("Deleting file from IDB failed.\n"); emscripten_fetch_close(fetch); }