2009-11-15 64 views
0

我想在内存中完全创建一个带.aspx扩展名(或任何其他扩展名)的文件。这可以做到吗?如何在内存中创建文件(.aspx,html,txt)?

现在我有一个内存流,有我想要写入这个文件的所有东西,但我实际上并不想在服务器上创建一个物理文件,因为我可能必须为我的服务器启用写入权限。我想要做的是在内存中创建文件并通过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"); 
} 

回答

5

您可以将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(); 
+0

这是不是在引擎盖下创建一个临时文件? – 2009-11-15 21:02:54

+0

不是我所知道的。为什么要这样? – dtb 2009-11-15 21:04:14

+0

首先是什么区别?我也不知道如何做到这一点为ftpWebRequest到目前为止我有这个 FtpWebRequest request2 =(FtpWebRequest)WebRequest.Create(path); request2.Method = WebRequestMethods.Ftp.UploadFile; request2.Credentials = new NetworkCredential(ftpUsername,ftpPassword); 现在我不知道如何写我的记忆流数据。 – chobo2 2009-11-15 21:04:25

0

好了,这样的文件实际上只是纯文本,具有一定的格式 - 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(); 
+0

那么我不想把它写到磁盘上。我想在扩展名的文件(不知道如何指定文件名和扩展名)文件中,然后我的代码我做了一个ftpWebRequest,并通过FTP上传文件。 – chobo2 2009-11-15 21:01:21

+0

如果你把它放在内存流中,那么就没有“文件名”或“扩展名”这样的东西 - 它只是流中的一串字节。 – 2009-11-15 21:41:10