转换为字节数组并保存到特定路径 [英] convert to byte array and save to specific path

查看:55
本文介绍了转换为字节数组并保存到特定路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我想将html文件保存到特定路径,而不需要保存文件对话框.

这是我用保存文件对话框保存文件的代码:

 SaveFileDialog对话框= 新建 SaveFileDialog();
dialog.DefaultExt = " ;
dialog.Filter = " ;

如果(dialog.ShowDialog()==  true )
{
RadDocument文档= CreateDocument(rgvReportData);

document.LayoutMode = DocumentLayoutMode.Paged;

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

使用(流输出= dialog.OpenFile())
{
provider.Export(文档,输出);
}
} 



我了解到可以将图像转换为字节数组,从而将图像保存到特定路径...

解决方案

首先,如SAKryukov所说,无需转换为字节数组,尽管您可能想使用字节数组进行缓冲. SaveFileDialog除了提供创建流的便捷方法外,不参与编写文件.摆脱1 st if语句并更改以下内容:

 使用(流输出= dialog.OpenFile())
{
  provider.Export(文档,输出);
} 



至:

 使用(流输出=  FileStream(  @"  FileMode.随便, FileAccess.随便)
{
  provider.Export(文档,输出);
} 


请注意,我为文件路径使用了魔术字符串,这很不好,您需要将其替换为文件名字符串.您还需要确定所需的FileModeFileAccess类型:
文件模式 [文件访问 [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?

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:

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



to:

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[^]


这篇关于转换为字节数组并保存到特定路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆