Currently i'm working on UWP apps, In that i need to download the pdf and word url to the system downloads folder, for that need i'm trying backgrounddownloader class as shown in below code.
Uri source = new Uri(selectedfile.DocumentPath);
StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(
selectedfile.DocumentName, CreationCollisionOption.GenerateUniqueName);
BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(source, destinationFile);
An also i'm trying another way to download the urls here is the code
var request = WebRequest.CreateHttp(selectedfile.DocumentPath);
var sampleFile = await KnownFolders.PicturesLibrary.CreateFileAsync(selectedfile.DocumentName, CreationCollisionOption.GenerateUniqueName);
var cli = new HttpClient();
var str = await cli.GetStreamAsync(request.RequestUri);
var dst = await sampleFile.OpenStreamForWriteAsync();
await str.AsInputStream().AsStreamForRead().CopyToAsync(dst);
For using above two methods url can be downloaded as pdf and word but i'm trying to open the pdf file and word file but it couldn't open, it showing error like as shown in below figure trying to open the open downloaded file it shown like this. Can any one help to solve this issue. I want to download the file and also downloaded file should be open.
The BackgroundDownloader.CreateDownload method initializes a DownloadOperation object that contains the specified Uri and the file that the response is written to.
When the DownloadOperation is created, it does not starts the download operation.
We should be able to use DownloadOperation.StartAsync to start the download operation.
For example:
Uri source = new Uri(selectedfile.DocumentPath);
StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync("ab.pdf", CreationCollisionOption.GenerateUniqueName);
BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(source, destinationFile);
await download.StartAsync();
If you want to know more detail about down load file, please refer the Background transfer sample.
Related
Directory:
App1
- MainPage.xaml.cs
- Sample.xaml
im trying to do is getting the xaml content from the sample as a string but it doesnt work since it cant find the file:
var x = Path.GetFullPath(#"sample.xaml");
FileStream s = new FileStream(x, FileMode.Open);
How can I fix this?
I haven't tried this give it a try.
Save your file in a location eg (Assets Folder)Now, make sure that the
build action is set to Content.
var storageFile = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appx:///Assets/youfile.xaml"));
The StorageFile you get is of course read-only, but it can be passed to any API that expects a StorageFile.
If you want to read you can try.
var result = storageFile.OpenReadAsync()
StorageFile Documentation
The source files (.xaml, .cs) are compiled, and in the deployed app they do not exist as physical files, so you can't open them this way.
I am trying to save an captured photo into a folder as following:
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200);
StorageFile photo = await
captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (photo == null)
{
// User cancelled photo capture
return;
}
StorageFolder destinationFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("ProfilePhotoFolder", CreationCollisionOption.OpenIfExists);
await photo.CopyAsync(destinationFolder, "ProfilePhoto.jpg", NameCollisionOption.ReplaceExisting);
await photo.DeleteAsync();
However, I cannot find the ProfilePhotoFolder in my file system. Could any one please tell me where is this folder. This is an Universal Windows project.
When you write to UWP LocalFolders the data is placed into Isolated Storage which is only accessible by your application, and thus you won't simply be able to fire up Windows Explorer and start digging into the files.
The recommended way for Windows 10 devices is to set your device to Developer Mode then use the web browser interface they've created (see https://blogs.windows.com/buildingapps/2016/06/08/using-the-app-file-explorer-to-see-your-app-data/#6O50PWljxSKfKCAm.97).
Try:
StorageFolder storageFolder = KnownFolders.PicturesLibrary;
Docs
I'm new to windows phone development, and not using silverlight or WPF. I copy the file "links.txt" into my Windows Phone folder at "\Documents\" and I want to access and get the content in the file, but I'm getting back as access denied error. I click on Package.appxmanifest file then select "Capabilities" tab, but I don't see "Documents Library Access" for me to check it. As matter fact I don't see any "... Library Access" showing. Below is the code:
string fileName = "\\Documents\\links.txt";
string parentPath = ApplicationData.Current.LocalFolder.Path;
string filePath = Path.Combine(parentPath, fileName);
StorageFile file = await StorageFile.GetFileFromPathAsync(filePath);
Any suggestion how can I read the file? thanks.
Updates:
It seems the code above does not work, but when using the code below and change the folder to "Music" instead of "Documents" then check the Capabilities for "MusicLibrary", it's working.
var folder = KnownFolders.MusicLibrary;
var file = await folder.GetFileAsync("links.txt");
var read = await FileIO.ReadTextAsync(file);
Thanks!
You are give the wrong filePath actual path is in this image
i try your code it shows same error so correct your file path
If you checked need Capability for Documents you should use KnownFolders class instead ApplicationData.Current.LocalFolder.Path.
For example:
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
StorageFile file = await storageFolder.CreateFileAsync("sample.dat", CreationCollisionOption.ReplaceExisting);
I am downloading and saving an image from internet, image gets saved into Saved Pictures folder.
Heres part of the code:
var imagefile = await KnownFolders.SavedPictures.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
var filestream = await imagefile.OpenAsync(FileAccessMode.ReadWrite);
var writer = new DataWriter(filestream.GetOutputStreamAt(0));
writer.WriteBytes(await response.Content.ReadAsByteArrayAsync());
I can see images there if I navigate to the folder via pc, they can be opened and seem to work just fine.
What am I missing?
using c#, VS2013, windows store app
In simple program have a file with some data saved in JSON format. I'll try to add new data to this file and then store it.
Try to save file in JSON with next code:
string result = await JsonConvert.SerializeObjectAsync(groups);
//get file path
string pathOfFile = Path.GetDirectoryName(file.Path); //get path
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(pathOfFile); //create dir by path
StringBuilder builder = new StringBuilder();
builder.Append(folder.Path);
builder.Append("\\EventsData.json");
Uri uriToSave = new Uri(builder.ToString());
//create file
StorageFile fileToWrite =
await StorageFile.GetFileFromApplicationUriAsync(uriToSave);
using (IRandomAccessStream stream =
await file.OpenAsync(FileAccessMode.ReadWrite))
{
// write the JSON file
using (DataWriter textWriter = new DataWriter(stream))
{
textWriter.WriteString(result);
await textWriter.StoreAsync();
}
}
But during executing code StorageFile fileToWrite = await StorageFile.GetFileFromApplicationUriAsync(uriToSave); got exception System.ArgumentException
During debagging got next Uri
Question - why i got such exception and if I'm wrong - how to save file in windows store app in required directory?
Also look MSDN sample for writing data to file, and use code like in tutorial but got System.UnauthorizedAccessException:
StorageFile sampleFile = await folder.CreateFileAsync("EventsData.json", CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteTextAsync(sampleFile, result);
Why Access denied or I missing something?
You cannot write to the install directory without explicit permission from the user.
Here is the protocol instead:
At first startup, check to see if there is a file in the local data folder (ApplicationDataContainer), if not, read in the file from the install directory.
Write out a copy of the file to the local app data folder (Check out guides on ApplicationDataContainer for more info there)
Edit this file freely.