In a Windows Store app project I was reading a File like this:
var uri = new Uri("ms-appx:///DataModel/Accounts.csv");
var file = await StorageFile.GetFileFromApplicationUriAsync(uri);
var read = await FileIO.ReadTextAsync(file);
The file Accounts.csv was added with build action content.
I have now moved the code to a separate class library. File is still added with build Action content.
But the uri scheme does not seem to work anymore.
I've also tried:
var file = await StorageFile.GetFileFromPathAsync("/DataModel/Accounts.csv");
var read = await FileIO.ReadTextAsync(file);
How do I access a file in a WinRT class library? Does the file library behave differently than the Store app project?
You can access file in Windows Store class library like this.
var _Assembly = Assembly.Load(new AssemblyName("ASSEMBLY_NAME"));
var _Stream = _Assembly.GetManifestResourceStream("YOUR_FILE_NAME_WITH_PATH.xml");
Please note in class library path members are seperated by . not by \
To get all the resource names
var names = _Assembly.GetManifestResourceNames();
The GetManifestResourceStream method will always returns NULL if the resource built action property is not set to embedded resource
Related
I am trying to use a Service Account to upload a file into a shared Google Drive folder, using Google Drive API v3 and the .Net client. The service account is added to the folder and "Can organise, add and edit". And if I do a list request I get the folder back (so I know the service account authentication works and that it at least has access to the shared folder).
If I try to upload a file though, I get a 200 response (so no error) but with an empty ResponseBody. And if I list again, the file isn't there. I don't understand what this means or why it is happening and can't find any information about this situation anywhere.
Here's my upload code. [PARENT_ID] is the ID of the shared folder and I've got the content of the CSV file I'm uploading and converted into bytes to put into the stream I've then sent.
using (var driveService = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = googleCredential.CreateScoped(DriveService.Scope.Drive),
ApplicationName = "FunctionApp"
}))
{
File fileMetadata = new File()
{
Name = "FileName",
MimeType = "application/vnd.google-apps.spreadsheet",
Parents = new List<string>() { "[PARENT_ID]" }
};
FilesResource.CreateMediaUpload request;
using (var csvStream = new MemoryStream())
{
csvStream.Write(csvBytes);
request = driveService.Files.Create(
fileMetadata, csvStream, "text/csv");
}
request.Fields = "id";
request.Upload();
var file = request.ResponseBody;
Console.WriteLine("File ID: " + file.Id);
}
The file variable is always null so obviously file.Id throws a NullReferenceException.
I've been staring at this for hours but have not been able to work out what is wrong. Any ideas?
I've tried saving the csv string to a file a then reading it in a file stream (closer to the example at the bottom of https://developers.google.com/drive/api/v3/manage-uploads#import_to_google_docs_types_) but this yields the same result.
Note: I've been able to create a file in the same folder using the "Try this API" tool on https://developers.google.com/drive/api/v3/reference/files/create using the parameters the same as what I pass in the metadata here, but of course that doesn't use a service account and doesn't pass any actual data to the file.
Thanks to #JonSkeet for the solution on GitHub (https://github.com/googleapis/google-api-dotnet-client/issues/1490)
The problem was that the stream was being disposed before the upload (ie after the using). The code change was to not use a using at all.
var csvStream = new MemoryStream(csvBytes);
request = driveService.Files.Create(fileMetadata, csvStream, "text/csv");
var result = request.Upload();
This did a successful upload as expected.
This also didn't use the request.Fields although this could maybe have been left in.
We are trying to get all node elements of DWFX file but we are getting undefined instance tree for DWFX file. We have used below code to get each element id.
// Try to get instance tree for DWFX file
var model = this.viewer.model;
var modelData = model.getData();
var it = modelData.instanceTree; // get instance tree
We have used another way to get element node id for DWFX file. (In that case, we are getting only panel label id for DWFX file) But that logic is not working for all DWFX files.
// Try to get all ids for DWFX file
var model = this.viewer.model;
var modelData = model.getData();
var allIds = modelData.stringDbIds; // get all ids
Please us know If I am using wrong approach to get all elements for DWFX file.
You need to wait for Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT event to make sure the instanceTree is available in your loaded model:
viewer.addEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT, function () {
var model = this.viewer.model;
var modelData = model.getData();
var it = modelData.instanceTree;
console.log(it)
})
In some cases you may have to wait also for Autodesk.Viewing.GEOMETRY_LOADED_EVENT event if you intend to access geometry of the components. Here is an article that may be relevant: Asynchronous viewer events notification
I am currently using IndexedDB on Firefox to store the output of some file processing that is being done by a web worker. I have managed to get the finished file saved within the IndexedDB as IDBMutableFile, however I can't seem to transfer the file as a blob to the web UI so that the user can download it.
When I read the file from the DB, I tried to use URL.createObjectURL to get a blob: link, however it seems like the file handle gets dereferenced as soon as the onsuccess handler exits (the blob is no longer accessible via a link).
I also tried to keep the file handle open using a FileReader, but that still didn't work (I get a FileHandleInactiveError).
Any ideas? The only thing that works seems to be to read the entire file as an arrayBuffer, and then create a new blob linked via URL.createObjectURL. The problem is that the file is very large (1GB) and loading the entire thing into memory causes an NS_ERROR_OUT_OF_MEMORY exception.
Here is the code:
// Retrieve the file that was just stored
transaction.objectStore("files").get("00b6ac47-7400-4894-8a76-de597c4d7b3e").onsuccess = function (event) {
var fileHandle = event.target.result;
var theFile = fileHandle.getFile();
theFile.onsuccess = function() {
var actualFile = this.result;
// Get window.URL object
var URL = window.URL || window.webkitURL;
var blobUrl = URL.createObjectURL(actualFile);
// Set img src to ObjectURL
var link = document.getElementById("blobLink");
blobLink.setAttribute("href", blobUrl);
//Problem: This link will point to a deleted blob in the UI
I'm trying to make my google docs script create a backup copy of the file each time I open it.
To make a copy I write
var name = File.getName();
var filecopy = File.makeCopy(name + " backup");
But it won't recognize the File class. Although it knows DocsList. How do I make it work or make a copy of the file another way?
GAS permits to call class methods or instance only native classes (Object, String, etc), own classes or Google Services (DocList, SpreadsheetApp, etc). Other classes like File, Folder, Spreadsheet, Range, etc are accessible and instanceable only via calling the services functions, for example, DocsList.getFileById("..."); returns the File class instance.
The following function copies a file having the srcFileID ID to a new file with the name stored in the dstFileName parameter.
function testCopy(srcFileID, dstFileName) {
var srcFile = DocsList.getFileById(srcFileID);
srcFile.makeCopy(dstFileName);
}
You cannot use the File class that way. Use something on these lines
var file = DocsList.getFileById(ID) ; // you can use DocsList.find or DocsList.create
var filecopy = file.makeCopy();
I need to use selenium to verify a download. I need to click on the download file link and check that it is downloadable or not. (Means download is starting or not)
I need to create a simple HTML script for this. But as Selenium does not recognize the 'Save As' dialog box for file download, I am not able to proceed.
Is there any solution within Selenium for this. I cannot use any other 3rd party tool as this is a part of centralized UI testing script.
Thanks in advance.
My solution (in C#) is get the Url of the file to download and any cookie and make the request using WebClient:
var testLink = seleniumDriver.FindElement(By.LinkText("Link to file"));
var pdfHref = testLink.GetAttribute("href");
var manage = seleniumDriver.Manage();
var cookies = manage.Cookies.AllCookies;
using (var wc = new WebClient())
{
foreach (var cookie in cookies)
{
var cookieText = cookie.Name + "=" + cookie.Value;
wc.Headers.Add(HttpRequestHeader.Cookie, cookieText);
}
var fileResult = wc.DownloadData(new Uri(pdfHref));
// or use wc.DownloadString or wc.DownloadFile
// Do any test required
}