繁体   English   中英

如何在内存中创建文件(.aspx,html,txt)?

[英]How to create a file(.aspx,html,txt) in memory?

我想在内存中完全创建一个扩展名为.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");
}

您可以将MemoryStream转换为byte[] ,然后使用WebClient.UploadData通过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();

嗯,这样的文件实际上只是纯文本,具有某些格式-HTML 4.0,XHTML或类似格式。

所以是的,您可以在内存中(例如在StringBuilder创建它,然后使用StreamWriter或其他方法将其保存回磁盘。

如果您不能或不希望将其写出到磁盘,则当然也可以将其放入MemoryStream ,并且可以从任意流读取的接口也可以从内存流读取。

在FtpWebRequest和GetRequestStream()方法上查看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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM