Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi!

I want to save an html file to a specific path without a save file dialog.

here's my code for saving the file with a savefile dialog:
C#
SaveFileDialog dialog = new SaveFileDialog();
dialog.DefaultExt = "*.html";
dialog.Filter = "WORD Document (*.html)|*.html";

if (dialog.ShowDialog() == true)
{
	RadDocument document = CreateDocument(rgvReportData);

	document.LayoutMode = DocumentLayoutMode.Paged;

	document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
	document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize));
	document.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(2, 2, 2, 2);
	document.SectionDefaultPageOrientation = PageOrientation.Landscape;
	
	HtmlFormatProvider provider = new HtmlFormatProvider();

	using (Stream output = dialog.OpenFile())
	{
		provider.Export(document, output);
	}
} 


i learned that i can save the image to a specific path by converting it to an array of byte...

how would i convert the html file that i created and store to a specific path?
Posted
Updated 30-May-11 21:54pm
v2
Comments
Sergey Alexandrovich Kryukov 30-May-11 22:33pm    
There is usually no need to convert HTML to an array of byte as this is a text file. Why what have you done doesn't work? Telerik documentation has a sample doing pretty much the same...
--SA
Member 7838027 30-May-11 22:40pm    
i want to save the file without savefile dialog... in order to do this, i have to convert the it to an array of byte? do you know of other ways to save a file without savefile dialog?
Sergey Alexandrovich Kryukov 30-May-11 23:10pm    
Not that one way or another. File dialog has nothing to do with saving data. Not at all, period. You're missing something very basic.
--SA

1 solution

First, as SAKryukov said, there is no need to convert to a byte array, although you might like to buffer using a byte array. The SaveFileDialog takes no part writing the file, other than providing a convenient way to create a stream. Get rid of your 1st if statement and change the following:

C#
using (Stream output = dialog.OpenFile())
{
  provider.Export(document, output);
}


to:

C#
using (Stream output = new FileStream(@"c:\foo.html",FileMode.Whatever, FileAccess.Whatever)
{
  provider.Export(document, output);
}

Note that I've used a magic string for the filepath, this is bad and you need to replace it with your filename string. You also need to determine which FileMode and FileAccess types you need:
FileMode[^]
FileAccess[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900