Winrt StreamWriter & StorageFile does not completely Overwrite File - xaml

Quick search here yielded nothing. So, I have started using some rather roundabout ways to use StreamWriter in my WinRT Application. Reading works well, writing works differently. What' I'm seeing is that when I select my file to write, if I choose a new file then no problem. The file is created as I expect. If I choose to overwrite a file, then the file is overwritten to a point, but the point where the stream stops writing, if the original file was large, then the old contents exist past where my new stream writes.
The code is as such:
public async void WriteFile(StorageFile selectedFileToSave)
{
// At this point, selectedFileToSave is from the Save File picker so can be a enw or existing file
StreamWriter writeStream;
Encoding enc = new UTF8Encoding();
Stream dotNetStream;
dotNetStream = await selectedFileToSave.OpenStreamForWriteAsync();
StreamWriter writeStream = new StreamWriter(dotNetStream, enc);
// Do writing here
// Close
writeStream.Write(Environment.NewLine);
await writeStream.FlushAsync();
await dotNetStream.FlushAsync();
}
Can anyone offer clues on what I could be missing? There are lots of functions missing in WinRT, so not really following ways to get around this

Alternatively you can set length of the stream to 0 with SetLength method before using StreamWriter:
var stream = await file.OpenStreamForWriteAsync();
stream.SetLength(0);
using (var writer = new StreamWriter(stream))
{
writer.Write(text);
}

Why not just use the helper methods in FileIO class? You could call:
FileIO.WriteTextAsync(selectedFileToSave, newTextContents);
If you really need a StreamWriter, first truncate the file by calling
FileIO.WriteBytesAsync(selectedFileToSave, new byte[0]);
And then continue with your existing code.

Related

.NET Core API saving image upload asynchronously with ImageSharp, MemoryStream and FileStream

I have a .NET Core API that I'd like to extend to save uploaded images asynchronously.
Using ImageSharp I should be able to check uploads and resize if predefined size limits are exceeded. However I can't get a simple async save working.
A simple (non-async) save to file works without problem:
My Controller extracts IFormFile from the upload and calls the following method without any problem
public static void Save(IFormFile image, string imagesFolder)
{
var fileName = Path.Combine(imagesFolder, image.FileName);
using (var stream = image.OpenReadStream())
using (var imgIS = Image.Load(stream, out IImageFormat format))
{
imgIS.Save(fileName);
}
}
ImageSharp is currently lacking async methods so a workaround is necessary.
The updated code below saves the uploaded file but the format is incorrect - when viewing the file I get the message "It appears we don't support this file format".
The format is extracted from the ImageSharp Load method. and used when saving to MemoryStream.
MemoryStream CopyToAsync method is used to save to FileStream to make the upload asynchronous.
public static async void Save(IFormFile image, string imagesFolder)
{
var fileName = Path.Combine(imagesFolder, image.FileName);
using (var stream = image.OpenReadStream())
using (var imgIS = Image.Load(stream, out IImageFormat format))
using (var memoryStream = new MemoryStream())
using (var fileStream = new FileStream(fileName, FileMode.OpenOrCreate))
{
imgIS.Save(memoryStream, format);
await memoryStream.CopyToAsync(fileStream).ConfigureAwait(false);
fileStream.Flush();
memoryStream.Close();
fileStream.Close();
}
}
I can't work out whether the issue is with ImageSharp Save to MemoryStream, or the MemoryStream.CopyToAsync.
I'm currently getting 404 on SixLabors docs - hopefully not an indication that the project has folded.
How can I make the upload async and save to file in the correct format?
CopyToAsync copies a stream starting at its current position. You must change the current position of memoryStream back to start before copying:
// ...
memoryStream.Seek(0, SeekOrigin.Begin);
await memoryStream.CopyToAsync(fileStream).ConfigureAwait(false);
// ...

Create a PDF file using PDFTron and then rename or delete it

I am trying to create PDF file using PDFTron in application which runs in the UWP environment. I am able to create a file successfully. Depending on user input that newly created file might need to be renamed or completely deleted from the system. Although when I try to access the file that was just created the system throws the following exception:
Exception thrown: 'System.IO.IOException' in System.IO.FileSystem.dll The process cannot access the file (filename) because it is being used by another process.
The following part show what is used for the file to be created:
await sdfDoc.SaveAsync(filePath, SDFDocSaveOptions.e_linearized, "%PDF-1.5");
sdfDoc.Dispose();
And this is my delete implementation:
var filedelete = Task.Run(() => File.Delete(filePath));
The creation of the file is running on a seperate Task and the deletion takes place upon a button press.
I understand the nature of the exception, although I was wondering if the resources of the file are returned to the system from PDFTron after the creation of the file?
Any help or direction would be much appreciated.
Thank you.
PDFNet uses reference counting internally to know when to release the filesystem handles and memory.
For example, the following would trigger the issue where the file is still locked.
PDFDoc doc = new PDFDoc(input_filename);
doc.InitSecurityHandler();
SDFDoc sdfdoc = doc.GetSDFDoc();
await sdfdoc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized, "%PDF-1.5");
sdfdoc.Dispose();
await Task.Run(() => File.Delete(output_file_path)); // fails, as PDFDoc still has reference.
But this would work as expected.
using(PDFDoc doc = new PDFDoc(input_filename))
{
doc.InitSecurityHandler();
SDFDoc sdfdoc = doc.GetSDFDoc();
await sdfdoc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized, "%PDF-1.5");
sdfdoc.Dispose();
}
await Task.Run(() => File.Delete(output_file_path)); // works
Note the using statement for the PDFDoc instance, and the manual dispose of the SDFDoc instance, though you could use a using statement on that also.

Rename azure blob file and Download it

I have few files in azure blobs that are stored with unique file names and when the client wants to download, i want to rename to a friendly name.
I'm still using 2014 azure storage dlls in my project and i'm not planning to update them anytime soon. So i can't use built-in ContentDeposition and rename it.
I tried using following code in my controller:
var blob = blobStorage.GetBlobRef("https://mysite.blob.core.windows.net/my-container/WERF3234435FFF_ERFas23E.doc");
MemoryStream memStream = new MemoryStream();
blob.DownloadToStream(memStream);
Response.ContentType = blob.Properties.ContentType;
Response.AddHeader("Content-Disposition", "Attachment; filename=abcd_New.doc");
Response.AddHeader("Content-Length", blob.Properties.Length.ToString());
Response.BinaryWrite(memStream.ToArray());
but its not downloading the file.
I also tried using this:
MemoryStream memStream = new MemoryStream();
blob.DownloadToStream(memStream);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ContentType = blob.Properties.ContentType;
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "Attachment; filename=" + friendlyName.doc);
System.Web.HttpContext.Current.Response.AddHeader("Content-Length", blob.Properties.Length.ToString());
System.Web.HttpContext.Current.Response.BinaryWrite(memStream.ToArray());
System.Web.HttpContext.Current.Response.End();
I have my business logic in a separate solution and getting the blob reference from there to my main solution.
Am i missing something?
When we're talking about ASP.NET MVC, I'm missing the Controller/Action in your code? You're not supposed to write to the HttpContext yourself when doing ASP.NET MVC. You have ActionResults for that.
public ActionResult Download()
{
// ...
var bytes = memStream.ToArray();
return File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, "abcd_New.doc");
}
The browser will decide whether to open the file download or open it directly within the browser window. If you want to control that, you will need the following piece of code before you call the return File(... method:
var contentDisposition = new System.Net.Mime.ContentDisposition
{
FileName = "abcd_New.doc",
Inline = false // true will try to open in Browser, false will download
};
Response.AppendHeader("Content-Disposition", contentDisposition.ToString());
We need to flush our response after wrote a file to response. I use the code which you provided. After adding following code, I can see the file can be download from server.
Response.BinaryWrite(memStream.ToArray());
Response.Flush();
Response.End();

How do stream a file from WCF that didn't originate from a filesystem. (sharepoint content database)

I'm trying to return a stream in WCF 3.5 using a REST-Style URL instead of SOAP. The idea is to read a file from SharePoint 2010, then pass it back to the client. (We have reasons for doing it this way instead of using SharePoint services, but I digress.) It appears as though the only way to send the file is by writing it to the filesystem using one FileStream, then using File.OpenRead to return the stream back to the client. Using a MemoryStream doesn't seem to work. IE prompts for the file save, but the file comes down as like 2KB and then can't be read of course because it's not all there. Any ideas?
SPListItemCollection lookupFld2 = docLibrary.GetItems(spQuery2);
if (lookupFld2.Count > 0)
{
WebOperationContext.Current.OutgoingResponse.ContentType =
"application/octet-stream";
WebOperationContext.Current.OutgoingResponse.Headers.Add(
"Content-Disposition","attachment; filename=" + lookupFld2[0].File.Name);
MemoryStream memoryStream =
(MemoryStream)lookupFld2[0].File.OpenBinaryStream();
memoryStream.Position = 0;
return memoryStream;
}
Technically, OpenBinaryStream only returns a general Stream instance NOT a MemoryStream (http://msdn.microsoft.com/en-us/library/ms470901.aspx). If you want a MemoryStream you need to create a new one and copy the contents from the BinaryStream into the MemoryStream then reset the position and return it.

Reading large csv files

Which is the most performant way to read a large csv file in .NET?
Using FileStream? or another class?
Thanks!
You can use the StreamReader returned by FileInfo.OpenText:
Dim file As New FileInfo("path\to\file")
Using reader As StreamReader = file.OpenText()
While Not reader.EndOfStream
Dim nextLine As String = reader.ReadLine()
ProcessCsvLine(nextLine)
End While
End Using
If you want to read it all into memory, a simple File.ReadAllText() will do just fine.
EDIT: If your file is indeed very large, then you can use the StreamReader class, see here for details. This approach is sometimes inevitable but should mostly be avoided for style reasons. See here for a more in-depth discussion.
The most efficient way of doing this is by taking advantage of deffered execution in LINQ. You can create a simple Linq-To-Text function that read one line at a time, work on it and then continue. This is really helpful since the file is really large.
I would desist from using the ReadBlock or ReadBlock or ReadToEnd methods of StreamReader class since they tend to read a number of lines at once or even the entire lines in the file. This ends up consuming more memory than if a line was read one at a time.
public static IEnumerable<string> Lines(this StreamReader source)
{
String line;
if (source == null)
throw new ArgumentNullException("source");
while ((line = source.ReadLine()) != null)
{
yield return line;
}
}
Note that the function is an extension method of the StreamReader class. This means it can be used as follows:
class Program
{
static void Main(string[] args)
{
using(StreamReader streamReader = new StreamReader("TextFile.txt"))
{
var tokens = from line in streamReader.Lines()
let items = line.Split(',')
select String.Format("{0}{1}{2}",
items[1].PadRight(16),
items[2].PadRight(16),
items[3].PadRight(16));
}
}
}
I had very good experience with this library:
http://www.codeproject.com/KB/database/CsvReader.aspx
I am using this library for Csv reading. This is really nice to use
http://www.codeproject.com/Articles/11698/A-Portable-and-Efficient-Generic-Parser-for-Flat-F