C#Asp.Net创建文本文件,将其压缩并保存到Blob,而无需将任何内容写入磁盘
这是一个复杂的问题,不管怎样对我来说:) 基本上,我想要实现的是生成一些文本,将这个文本文件压缩到两个目录中,然后将其上传到MySQL blob字段中——所有这些都不需要向磁盘写入任何内容。我对所有这些都比较陌生,所以非常感谢任何指点。这是我到目前为止所做的,它显然会崩溃和燃烧,但希望能让我更好地了解我想做什么。哦,我现在正在使用DotNetZip:) ***编辑-靠近*** 我现在可以创建一个文本文件并将其压缩到内存中,问题是文本没有显示在文件中-即它是空白的,我现在将该文件放在两个目录中:) 修订守则如下C#Asp.Net创建文本文件,将其压缩并保存到Blob,而无需将任何内容写入磁盘,c#,asp.net,mysql,zip,streamwriter,C#,Asp.net,Mysql,Zip,Streamwriter,这是一个复杂的问题,不管怎样对我来说:) 基本上,我想要实现的是生成一些文本,将这个文本文件压缩到两个目录中,然后将其上传到MySQL blob字段中——所有这些都不需要向磁盘写入任何内容。我对所有这些都比较陌生,所以非常感谢任何指点。这是我到目前为止所做的,它显然会崩溃和燃烧,但希望能让我更好地了解我想做什么。哦,我现在正在使用DotNetZip:) ***编辑-靠近*** 我现在可以创建一个文本文件并将其压缩到内存中,问题是文本没有显示在文件中-即它是空白的,我现在将该文件放在两个目录中:)
public void test3()
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.WriteLine("HELLO!");
sw.WriteLine("I WANT TO SAVE THIS FILE AS A .TXT FILE WITHIN TWO FOLDERS");
ms.Position = 0;
// create the ZipEntry archive from the xml doc store in memory stream ms
MemoryStream outputMS = new System.IO.MemoryStream();
ZipOutputStream zipOutput = new ZipOutputStream(outputMS);
ZipEntry ze = new ZipEntry(@"Directory1\Directory2\example.txt");
zipOutput.PutNextEntry(ze);
zipOutput.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
zipOutput.Finish();
zipOutput.Close();
byte[] byteArrayOut = outputMS.ToArray();
outputMS.Close();
ms.Close();
try
{
OdbcConnection rstServer = new OdbcConnection("DSN=CentralServer");
Server.Open();
OdbcCommand DbCommand = Server.CreateCommand();
DbCommand.CommandText = "INSERT INTO blobtest(blobfield) VALUES(?)";
OdbcParameter param = new OdbcParameter("@file", SqlDbType.Binary);
param.Value = byteArrayOut;
DbCommand.Parameters.Add(param);
DbCommand.ExecuteNonQuery();
Response.Write(byteArrayOut.ToString());
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
您可以使用开源c#压缩库SharpZipLib创建内存中的zip文件,如下所述:
现在您只需要将内存流转换为字节数组并将其保存在数据库中 基本上,这是我想要实现的全部编译,所以我想我会为任何需要类似东西的人把它放在一起——这是一段工作代码
public void diskLess()
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.WriteLine("HELLO!");
sw.WriteLine("I WANT TO SAVE THIS FILE AS A .TXT FILE WITHIN TWO FOLDERS");
sw.Flush(); //This is required or you get a blank text file :)
ms.Position = 0;
// create the ZipEntry archive from the txt file in memory stream ms
MemoryStream outputMS = new System.IO.MemoryStream();
ZipOutputStream zipOutput = new ZipOutputStream(outputMS);
ZipEntry ze = new ZipEntry(@"dir1\dir2\whatever.txt");
zipOutput.PutNextEntry(ze);
zipOutput.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
zipOutput.Finish();
zipOutput.Close();
byte[] byteArrayOut = outputMS.ToArray();
outputMS.Close();
ms.Close();
try
{
OdbcConnection rstServer = new OdbcConnection("DSN=CentralServer");
Server.Open();
OdbcCommand DbCommand = Server.CreateCommand();
DbCommand.CommandText = "INSERT INTO blobtest(blobfield) VALUES(?)";
OdbcParameter param = new OdbcParameter("@file", SqlDbType.Binary);
param.Value = byteArrayOut;
DbCommand.Parameters.Add(param);
DbCommand.ExecuteNonQuery();
Response.Write(byteArrayOut.ToString());
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
哪个部分崩溃,如果有错误消息/堆栈跟踪呢?在此版本中,我无法访问zip.Save(ms2)上的封闭流-但我不确定我是否使用此方法在正确的路径上:(SubmissionReceiptListAttachmentMTOM在最新版本中似乎不存在。我跟踪了您的代码并在编辑中进行了必要的更改-知道文本文件为何为空吗?
// zip XElement xdoc and add to requests MTOM value
using (MemoryStream ms = new System.IO.MemoryStream())
{
xdoc.Save(ms);
ms.Position = 0;
// create the ZipEntry archive from the xml doc store in memory stream ms
using (MemoryStream outputMS = new System.IO.MemoryStream())
{
using (ZipOutputStream zipOutput = new ZipOutputStream(outputMS))
{
ZipEntry ze = new ZipEntry("example.xml");
zipOutput.PutNextEntry(ze);
zipOutput.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
zipOutput.Finish();
zipOutput.Close();
// add the zip archive to the request
SubmissionReceiptListAttachmentMTOM = new base64Binary();
SubmissionReceiptListAttachmentMTOM.Value = outputMS.ToArray();
}
outputMS.Close();
}
ms.Close();
}
public void diskLess()
{
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.WriteLine("HELLO!");
sw.WriteLine("I WANT TO SAVE THIS FILE AS A .TXT FILE WITHIN TWO FOLDERS");
sw.Flush(); //This is required or you get a blank text file :)
ms.Position = 0;
// create the ZipEntry archive from the txt file in memory stream ms
MemoryStream outputMS = new System.IO.MemoryStream();
ZipOutputStream zipOutput = new ZipOutputStream(outputMS);
ZipEntry ze = new ZipEntry(@"dir1\dir2\whatever.txt");
zipOutput.PutNextEntry(ze);
zipOutput.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length));
zipOutput.Finish();
zipOutput.Close();
byte[] byteArrayOut = outputMS.ToArray();
outputMS.Close();
ms.Close();
try
{
OdbcConnection rstServer = new OdbcConnection("DSN=CentralServer");
Server.Open();
OdbcCommand DbCommand = Server.CreateCommand();
DbCommand.CommandText = "INSERT INTO blobtest(blobfield) VALUES(?)";
OdbcParameter param = new OdbcParameter("@file", SqlDbType.Binary);
param.Value = byteArrayOut;
DbCommand.Parameters.Add(param);
DbCommand.ExecuteNonQuery();
Response.Write(byteArrayOut.ToString());
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}