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

本文介绍如何在内存中创建扩展名为.aspx的文件,并通过FTPWebRequest上传至服务器。讨论了编码方式对文件内容的影响及如何正确处理内存流。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值