C# 如何在内存中创建文件(.aspx、html、txt)?
我想在内存中创建一个扩展名为.aspx(或任何其他扩展名)的文件。这能做到吗 现在,我有一个内存流,其中包含了我想写入此文件的所有内容,但实际上我不想在服务器上创建物理文件,因为那时我可能需要为我的服务器启用写入权限。我想做的是在内存中创建文件并通过ftpWebRequest上传 编辑 我一定是做错了什么,因为我的文件里有奇怪的东西,我甚至不能把它粘贴到我的帖子里 基本上,这是一个正方形之间的一切。似乎它几乎似乎填补了空间。如果我仔细看,我仍然会看到标签,但每个字母之间都有一个正方形 这是我代码的一部分。也许我使用了错误的编码C# 如何在内存中创建文件(.aspx、html、txt)?,c#,.net,asp.net,asp.net-mvc,C#,.net,Asp.net,Asp.net Mvc,我想在内存中创建一个扩展名为.aspx(或任何其他扩展名)的文件。这能做到吗 现在,我有一个内存流,其中包含了我想写入此文件的所有内容,但实际上我不想在服务器上创建物理文件,因为那时我可能需要为我的服务器启用写入权限。我想做的是在内存中创建文件并通过ftpWebRequest上传 编辑 我一定是做错了什么,因为我的文件里有奇怪的东西,我甚至不能把它粘贴到我的帖子里 基本上,这是一个正方形之间的一切。似乎它几乎似乎填补了空间。如果我仔细看,我仍然会看到标签,但每个字母之间都有一个正方形 这是我代码
using (MemoryStream memory = new MemoryStream())
{
UnicodeEncoding uniEncoding = new UnicodeEncoding();
// readByline is the first bunch of data I want for my new file.
memory.Write(uniEncoding.GetBytes(readByLine), 0, readByLine.Length);
// second bunch of data I want for my new file.
memory.Write(uniEncoding.GetBytes(html), 0, html.Length);
// the follow code just figure out the end of the file that I am
// trying to extract some information out of.
string readToEnd = reader.ReadToEnd();
int endIndex = readToEnd.IndexOf(END_FLAG);
endIndex += END_FLAG.Length;
string restOfFile = readToEnd.Substring(endIndex);
// once found I write it the memory stream.
memory.Write(uniEncoding.GetBytes(restOfFile),0,restOfFile.Length);
// now I want to upload my file. I have the same file name already
// existing on the server? Do I have to tell it override it?
FtpWebRequest request2 = (FtpWebRequest)WebRequest.Create(path);
request2.Method = WebRequestMethods.Ftp.UploadFile;
request2.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
// now I am trying your code.
byte[] fileContents = memory.ToArray();
using (Stream writer = request2.GetRequestStream())
{
writer.Write(fileContents, 0, fileContents.Length);
}
FtpWebResponse test = (FtpWebResponse)request2.GetResponse();
return Content("test");
}
嗯,这样的文件实际上只是纯文本,具有特定的格式—HTML4.0、XHTML或类似的格式 因此,是的,您可以在内存中创建,例如在
StringBuilder
中,然后使用StreamWriter或其他方式将其保存回磁盘
如果您不能或不想将其写入磁盘,当然也可以将其放入内存流
,可以从任意流读取的接口也可以从内存流读取
查看FtpWebRequest和on方法上的MSDN文档。它有一个关于如何从流直接上传到FTP的示例:
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.UploadFile;
// this could be your MemoryStream, of course, that you're reading from
StreamReader sourceStream = new StreamReader(fileName);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
// check the response, do whatever you need to do with it
response.Close();
嗯,这样的文件实际上只是纯文本,具有特定的格式—HTML4.0、XHTML或类似的格式 因此,是的,您可以在内存中创建,例如在
StringBuilder
中,然后使用StreamWriter或其他方式将其保存回磁盘
如果您不能或不想将其写入磁盘,当然也可以将其放入内存流
,可以从任意流读取的接口也可以从内存流读取
查看FtpWebRequest和on方法上的MSDN文档。它有一个关于如何从流直接上传到FTP的示例:
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.UploadFile;
// this could be your MemoryStream, of course, that you're reading from
StreamReader sourceStream = new StreamReader(fileName);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
// check the response, do whatever you need to do with it
response.Close();
您可以将MemoryStream转换为
字节[]
,然后使用FTP将文件上载到某个服务器,而无需先在客户端将其写入磁盘:
webClient.UploadData(
"ftp://remoteserver/remotepath/file.aspx"
memoryStream.ToArray());
当然,FtpWebRequest也可以工作,但还需要几行代码:
FtpWebRequest ftpRequest;
FtpWebResponse ftpResponse;
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://..."));
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.Proxy = null;
ftpRequest.UseBinary = true;
ftpRequest.Credentials = new NetworkCredential("UserName", "Password");
using (Stream stream = ftpRequest.GetRequestStream())
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
{
writer.WriteLine("<html><head><title>Hello World</title></head>...");
}
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
FtpWebRequest ftpRequest;
FtpWebResponse-ftpResponse;
ftpRequest=(FtpWebRequest)FtpWebRequest.Create(新Uri(“ftp://..."));
ftpRequest.Method=WebRequestMethods.Ftp.UploadFile;
ftpRequest.Proxy=null;
ftpRequest.UseBinary=true;
ftpRequest.Credentials=新的网络凭据(“用户名”、“密码”);
使用(Stream-Stream=ftpRequest.GetRequestStream())
使用(StreamWriter=newstreamwriter(stream,Encoding.UTF8))
{
WriteLine(“你好,世界……”);
}
ftpResponse=(FtpWebResponse)ftpRequest.GetResponse();
您可以将MemoryStream转换为字节[]
,然后使用FTP将文件上载到某个服务器,而无需先在客户端将其写入磁盘:
webClient.UploadData(
"ftp://remoteserver/remotepath/file.aspx"
memoryStream.ToArray());
当然,FtpWebRequest也可以工作,但还需要几行代码:
FtpWebRequest ftpRequest;
FtpWebResponse ftpResponse;
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://..."));
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.Proxy = null;
ftpRequest.UseBinary = true;
ftpRequest.Credentials = new NetworkCredential("UserName", "Password");
using (Stream stream = ftpRequest.GetRequestStream())
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
{
writer.WriteLine("<html><head><title>Hello World</title></head>...");
}
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
FtpWebRequest ftpRequest;
FtpWebResponse-ftpResponse;
ftpRequest=(FtpWebRequest)FtpWebRequest.Create(新Uri(“ftp://..."));
ftpRequest.Method=WebRequestMethods.Ftp.UploadFile;
ftpRequest.Proxy=null;
ftpRequest.UseBinary=true;
ftpRequest.Credentials=新的网络凭据(“用户名”、“密码”);
使用(Stream-Stream=ftpRequest.GetRequestStream())
使用(StreamWriter=newstreamwriter(stream,Encoding.UTF8))
{
WriteLine(“你好,世界……”);
}
ftpResponse=(FtpWebResponse)ftpRequest.GetResponse();
我现在还不想把它写到磁盘上。我想把它放在一个扩展名为的文件中(不知道如何指定文件名和扩展名),然后我的代码我做了一个ftpWebRequest,并将通过ftp上传文件。如果你把它放在内存流中,就没有“文件名”或“扩展名”这样的东西-它只是一个流中的一堆字节。我现在还不想把它写到磁盘上。我想把它放在一个扩展名为的文件中(不知道如何指定文件名和扩展名),然后我的代码我做了一个ftpWebRequest,并将通过ftp上传文件。如果你把它放在内存流中,就没有“文件名”或“扩展名”这样的东西-它只是一个流中的一堆字节。这不是在后台创建临时文件吗?我不知道。为什么呢?首先有什么区别?我也不知道如何为ftpWebRequest做到这一点,到目前为止,我有这个ftpWebRequest2=(ftpWebRequest)WebRequest.Create(path);request2.Method=WebRequestMethods.Ftp.UploadFile;request2.Credentials=新的网络凭据(ftpUsername、ftpPassword);现在我不知道如何将我的memorystream数据写入其中。忽略我所说的,最终-物理文件将以字节流的形式发送。;-)和+1顺便说一句-我不知道上传数据中有这种整洁的重载。这不会在后台创建临时文件吗?我不知道。为什么呢?首先有什么区别?我也不知道如何为ftpWebRequest做到这一点,到目前为止,我有这个ftpWebRequest2=(ftpWebRequest)WebRequest.Create(path);request2.Method=WebRequestMethods.Ftp.UploadFile;request2.Credentials=新的网络凭据(ftpUsername、ftpPassword);现在我不知道如何将我的memorystream数据写入其中。忽略我所说的,最终-物理文件将以字节流的形式发送。;-)和+1顺便说一句-没有意识到上传数据中的这种整洁过载。