开发者

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

开发者 https://www.devze.com 2022-12-11 17:59 出处:网络
I want to create a file with a .aspx extension(or any other extension) completely in memory. Can this be done?

I want to create a file with a .aspx extension(or any other extension) completely in memory. Can this be done?

Right now I have a memory stream that has all the stuff I want to write to this file but I don't actually want to create a physical file on the server since then I probably have to enable write permissions for my server. What I want to do is create the file in memory and upload via ftpWebRequest.

Edit.

I must be doing something wrong because I get weird stuff in my file so weird I can't even paste it into my posts.

Basically it is a bunch of squares between everything. Like it almost seems like it fills it in for the spaces. Like If I look closely I will see the tag still but it will have a square between each letter.

Here is a portion of my code. Maybe I am using the wrong encoding?

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 fil开发者_开发百科e 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");
}


You can convert the MemoryStream to a byte[] and then use WebClient.UploadData to upload the file with FTP to some server without writing it on the client to disk first:

webClient.UploadData(
    "ftp://remoteserver/remotepath/file.aspx"
    memoryStream.ToArray());

FtpWebRequest works as well, of course, but needs a few more lines of code:

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();


Well, such a file is really just plain text, with certain formatting - HTML 4.0, XHTML or such.

So yes, you can create that in memory - inside e.g. a StringBuilder, and then save it back out to disk using a StreamWriter or some other means.

If you can't or don't want to write it out to disk, you can of course also put it into a MemoryStream, and interfaces that can read from any arbitrary stream can read from the memory stream, too.

Check out the MSDN docs on FtpWebRequest and on GetRequestStream() method. It has a sample on how to upload from a stream to FTP directly:

// 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();
0

精彩评论

暂无评论...
验证码 换一张
取 消