I am launching document from isolated storage, when I debug on next code, previous stream gets closed and getting this exception.
"File-Name" has been damaged and can't be opened.
See my code below:
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (stream = storageFile.OpenFile("Document.docx", FileMode.Create))
{
await stream.WriteAsync(buffer, 0, buffer.Length);
}
}
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile pdffile = await local.GetFileAsync("Document.docx");
await Windows.System.Launcher.LaunchFileAsync(pdffile);
1) Make Sure If you are Downloading file from the URI than you should use WebClient instead of HttpWebRequest
2) Make sure you paas the correct URI
Related
i'm trying to create an app that downloads a file and then edits this file.
The Problem Im having is once the file is downloaded it doesn't seem to let go of that file, i can download the file to its local storage, i have gotten the file manually from the Iso and its fine. if i use the app to proceed after downloading the file i get the System.UnauthorizedAccessException error, but if i close and open the app and then just edit the file saved in iso it works, like i said its like something is still using the downloaded file.
public async void DownloadTrack(Uri SongUri)
{
var httpClient = new HttpClient();
var data = await httpClient.GetByteArrayAsync(SongUri);
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("Test.mp3", CreationCollisionOption.ReplaceExisting);
var targetStream = await file.OpenAsync(FileAccessMode.ReadWrite);
await targetStream.AsStreamForWrite().WriteAsync(data, 0, data.Length);
await targetStream.FlushAsync();
}
this code works fine to download the mp3, as ive tested the download file. I have seen if examples where the code ends with
targetStream.Close();
but it doesnt give me that, is there another way to close the download
thanks.
Instead of calling Close() or Dispose() I really like to use using which does the job automatically. So your method could look like this:
public async void DownloadTrack(Uri SongUri)
{
using (HttpClient httpClient = new HttpClient())
{
var data = await httpClient.GetByteArrayAsync(SongUri);
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("Test.mp3", CreationCollisionOption.ReplaceExisting);
using (var targetStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
await targetStream.AsStreamForWrite().WriteAsync(data, 0, data.Length);
await targetStream.FlushAsync();
}
}
}
I am currently developing a Windows 8.1 Application using C# and XAML in which I store a huge amount of data using XML format in the local cache of the application. I am getting the following error with one of the XML files out of three since the other two are being created: "Error:There was an error generating the XML document." Below is my code:
if (!string.IsNullOrEmpty(localData2))
{
StorageFile localFile = await ApplicationData.Current.LocalFolder
.CreateFileAsync("LocalCache2.xml", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(localFile,
ObjectSerializer<ObservableCollection<aceapp.Competitor>>.ToXml(cacheXML2));
}
if (!string.IsNullOrEmpty(localData3))
{
StorageFile localFile = await ApplicationData.Current.LocalFolder
.CreateFileAsync("LocalCache3.xml", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(localFile,
ObjectSerializer<ObservableCollection<aceapp.Attributes>>.ToXml(cacheXML3));
}
if (!string.IsNullOrEmpty(localData))
{
MemoryStream stream = new MemoryStream();
StorageFile localFile = await ApplicationData.Current.LocalFolder
.CreateFileAsync("LocalCache1.xml", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(localFile,
ObjectSerializer<ObservableCollection<aceapp.Sector>>.ToXml(cacheXML1));
}
The Error is with the last XML which is the largest one.
Any idea about this issue and how to fix it?
I would like to show a pdf from a website in my windows rt app (for Desktops with windows 8). How can I handle this?
If I add the file manually to the asset I can display it. But how can I do this with files where are not located on my asset Folder?
Can I copy the file to the StorageFolder?
Or can I directly open the file from the website?
Please give me some hints how I can solve this.
THX
To open files from you application you use Launcher in Windows 8 Applications. Refer the MSDN LINK: http://msdn.microsoft.com/en-in/library/windows/apps/hh701465.aspx and http://www.codeguru.com/win_mobile/win_store_apps/launching-files-with-associated-programs-in-windows-8.x-and-vb.htm
Hi I want to open it in my app and I not want to use the launcher.
I copy the file with HttpWebRequest to LocalFolder and process the pdf. I've solved it this way:
StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile file = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(filepath);
//TODO Systemuser
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
var response = await request.GetResponseAsync();
List<Byte> allBytes = new List<byte>();
using (Stream imageStream = response.GetResponseStream()) {
byte[] buffer = new byte[4000];
int bytesRead = 0;
while ((bytesRead = await imageStream.ReadAsync(buffer, 0, 4000)) > 0) {
allBytes.AddRange(buffer.Take(bytesRead));
}
}
file = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBytesAsync(file, allBytes.ToArray());
await RenderPDFPage(fileName);
I need to save Image from Local Storage Folder to file selected from SavePicker.
Should I use stream?
Here is my code:
StorageFile file = await savePicker.PickSaveFileAsync();
if (null != file)
{
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile current_photo = await localFolder.GetFileAsync(img.Tag.ToString());
// TODO Stream from current_photo to file
}
CopyAsync should do it:
await current_photo.CopyAndReplaceAsync(file);
I'm currently trying to save an stream containing a jpeg image I got back from the camera to the local storage folder. The files are being created but unfortunately contain no data at all. Here is the code I'm trying to use:
public async Task SaveToLocalFolderAsync(Stream file, string fileName)
{
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile storageFile = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream fileStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0))
{
using (DataWriter dataWriter = new DataWriter(outputStream))
{
dataWriter.WriteBytes(UsefulOperations.StreamToBytes(file));
await dataWriter.StoreAsync();
dataWriter.DetachStream();
}
await outputStream.FlushAsync();
}
}
}
public static class UsefulOperations
{
public static byte[] StreamToBytes(Stream input)
{
using (MemoryStream ms = new MemoryStream())
{
input.CopyTo(ms);
return ms.ToArray();
}
}
}
Any help saving files this way would be greatly appreciated - all help I have found online refer to saving text. I'm using the Windows.Storage namespace so it should work with Windows 8 too.
Your method SaveToLocalFolderAsync is working just fine. I tried it out on a Stream I passed in and it copied its complete contents as expected.
I guess it's a problem with the state of the stream that you are passing to the method. Maybe you just need to set its position to the beginning beforehand with file.Seek(0, SeekOrigin.Begin);. If that doesn't work, add that code to your question so we can help you.
Also, you could make your code much simpler. The following does exactly the same without the intermediate classes:
public async Task SaveToLocalFolderAsync(Stream file, string fileName)
{
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile storageFile = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (Stream outputStream = await storageFile.OpenStreamForWriteAsync())
{
await file.CopyToAsync(outputStream);
}
}