How to use WriteAllBytes if using UnityWebRequest? - unity3d

I use UnityWebRequest in getting the file on streamingassets folder because WWW is obsolete, now on FileWriteAllBytes i cant right downloader.bytes because it is return an error to me can someone help me
#if UNITY_ANDROID
// path on streamingasset folder
string path = System.IO.Path.Combine(Application.streamingAssetsPath, "game.dat");
UnityWebRequest downloader = UnityWebRequest.Get(path);
yield return downloader.SendWebRequest();
while (!downloader.isDone)
{
// nothing to be done while downloader gets our db file
}
// then save to application.persistentdatapath
// on this second param i dont know what do right i cant put donwloader.bytes because is obsolete
File.WriteAllBytes(dataFilePath, missing);
#endif

use DownloadHandler
UnityWebRequest downloader = UnityWebRequest.Get(path);
yield return downloader.SendWebRequest();
DownloadHandler handler = downloader.downloadHandler;
File.WriteAllBytes(dataFilePath, handler.data);

Related

FileSystemException: Cannot open file, path = 'storage/emulated/0/DCIM/docs/myPdf.pdf' (OS Error: Operation not permitted, errno = 1)

I am trying to create a pdf document and write my data in that file. The data is a list of int or uint type. Able to do the same for images but not pdf or doc file. All permissions are given and it works for images. My code is below -
Future <File> createDocFile(Uint8List fileData, String type) async
{
File file = new File(".pdf");
if(await PermissionHandler.checkPermission(Permission.storage)!=true){
Url.toastShow("Storage permission not found.");
await Permission.storage.request();
}
else {
await file.writeAsBytes(fileData);
print("PDF SAVED IN DEVICE");
Url.toastShow("PDF saved in device",Colors.green);
}
}
Whenever you create a new file the device can't automatically find it. You'd have to manually tell the device to refresh for the files. Before you'd have to refresh all the files in the device for it to get updated which was very inefficient, but now you could just send the path of the file that you want to get updated. You can use the media_scanner plugin to do so.
Or If you wanna do it yourself with kotlin then here's the code,
private fun broadcastFileUpdate(path: String){
context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(File(path))))
}

not able to download file with size more than 63kb using UnityWebRequest

i am try to download asset bundle from an url but the request keep on cancelling after downloading 63kb. Can anyone explain to me why this may be happening?
My Code :
public IEnumerator DL()
{
string downloadlink = "https://drive.google.com/file/d/1OGyrB4-MQfo-HVom9ENvV4dn312_wL4Q/view?usp=sharing";
string filepath = Application.persistentDataPath + "/electroplatingNN";
//Download
UnityWebRequest dlreq = new UnityWebRequest(downloadlink);
dlreq.downloadHandler = new DownloadHandlerFile(filepath);
dlreq.timeout = 15;
UnityWebRequestAsyncOperation op = dlreq.SendWebRequest();
while (!op.isDone)
{
//here you can see download progress
Debug.Log(dlreq.downloadedBytes / 1000 + "KB");
yield return null;
}
if (dlreq.isNetworkError || dlreq.isHttpError)
{
Debug.Log(dlreq.error);
}
else
{
Debug.Log("download success");
}
dlreq.Dispose();
yield return null;
}
As already mentioned in the comments the issue is not in the UnityWebrequest but rather Google Drive doesn't simply download your file as you expect.
Instead the data you download is actually only the web page which would allow you to download it. If I simply open your file in a browser I get a page looking like this
where I now could download your file.
There are packages like this or this which implement the necessary stuff for actually download files from Google Drive API in Unity.

How to get path of resource file and return it from jar file in eclipse plugin

Here I am trying to add Help .html files on Cntl+space. Its working on my laptop because I have source code but not on server. No error but not returning path on server.
Here is my code:
Bundle bundle=Platform.getBundle("my plugin ID");
URL url = FileLocator.find(bundle, new Path("/doc/myfile/file/"), null);
//Here in file folder my html files.
String fPath = "";
try {
URL fileURL = FileLocator.toFileURL(url);
fPath=fileURL.getPath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String fullPath = filePath + name+"."+ myfileextension;
return fullPath;//returning myfile path
You must give FileLocator.toFileURL the full name of the file you want, you can't use just the folder name.
This is because FileLocator.find may have to extract the file from the plug-in jar to a temporary location to make it available.
So:
URL url = FileLocator.find(bundle, new Path("/doc/myfile/file/" + name + "." + myfileextension), null);
Also check your build.properties file. You must include all the folders you want to use in the plugin in this.

Load object from Asset Bundle

I have Download and Main cs files. The first one contains method LoadAsset
public IEnumerator LoadAsset(string link, string loadObject)
{
WWW download;
string ErrorMsg=" ";
download = new WWW(link);
yield return download;
if (download.error != null)
{
ErrorMsg=download.error;
}
else
{
ErrorMsg="Downloading ....";
AssetBundle asset = download.assetBundle;
GameObject loadedObject;
loadedObject = Instantiate(asset.Load(loadObject,typeof(GameObject))) as GameObject;
asset.Unload(false);
ErrorMsg="Done";
}
Debug.Log(ErrorMsg);
}
I want to call this method from Main.cs file and return loadedObject in it. Tried to use StartCoroutine
Download download;
download=new Download();
StartCoroutine("download.LoadAsset()");
but without success. Can anybody help me?
Try without the quotes
StartCoroutine( download.LoadAsset() )
You add the quotes when you want to cancel it, but it won't work on a method outside your class.
It would be event better to add a method to your Main.cs that starts the coroutine for you, something like this
public void StartLoadAssetCo()
{
StartCoroutine(LoadAsset());
}
and then just call StartLoadAssetCo from outside the class
download.StartLoadAssetCo();

FileReader returns empty result for file from the bundle

I've tried to read a file from the app bundle using phonegap's FileReader class:
...
loadFile: function (path, callback) {
fileReader = new FileReader();
fileReader.onerror = function () {
...
}
fileReader.onload = function (evt) {
callback(evt.target.result);
}
fileReader.readAsText("./www/" + path);
}
In this example path is something like "index.html". The onerror callback is never called. onload is called but evt.target.result is empty. Do you have any suggestions? Is it in general possible to read files from the bundle with the phonegap API? Can I use relative paths like "./www/foo.txt"?
Thanks for your answers!
The path that is passed into readAsText is relative to the "Documents" folder in the applications sandbox. Hence you have to simply fix the path by replacing the line
fileReader.readAsText("./www/" + path);
with
fileReader.readAsText("./../myApp.app/www/" + path);
to access the file. This works for me.