views:

160

answers:

2

Hi

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 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");
}
A: 

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();
marc_s
Well I don't want to write it to disk just yet. I want to make it in a file with the extension(not sure how to specify the file name and givet hat extension) then my code I have make a ftpWebRequest and would upload the file via ftp.
chobo2
If you have it in a memory stream, there's no such thing as a "filename" or "extension" - it's just a bunch of bytes in a stream.
marc_s
+4  A: 

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();
dtb
Does that not create a temp file under the hood?
Wim Hollebrandse
Not that I'm aware of. Why should it?
dtb
whats the difference first of all? I am also not sure how to do this for ftpWebRequest so far I have this FtpWebRequest request2 = (FtpWebRequest)WebRequest.Create(path); request2.Method = WebRequestMethods.Ftp.UploadFile; request2.Credentials = new NetworkCredential(ftpUsername, ftpPassword);Now I am not sure how to write my memorystream data to it.
chobo2
Ignore what I said, ultimately - the physical file will be sent as byte stream anyway. ;-)
Wim Hollebrandse
and +1 btw - wasn't aware of this neat overload in UploadData.
Wim Hollebrandse
Hmm I doing something wrong. See my edit to see what I have.
chobo2
If you see funny characters then it's most likely an encoding issue. The `UnicodeEncoding` class implements a UTF-16 encoding, sou you'll have to view the file with a viewer that interprets the file as UTF-16 as well.
dtb
Ya I changed it to utf-8. All these different types of encoding confuses me lol.
chobo2
So what is the different from webClient.upload and ftpWebRequest?
chobo2