Asp.net 如何从fileupload控件设置Filestream?
这是我的代码,我似乎无法将我的FileUploadCotrol中的文件放入FILESTREAMAsp.net 如何从fileupload控件设置Filestream?,asp.net,visual-studio-2010,c#-4.0,asp.net-4.0,Asp.net,Visual Studio 2010,C# 4.0,Asp.net 4.0,这是我的代码,我似乎无法将我的FileUploadCotrol中的文件放入FILESTREAM // The buffer size is set to 2kb int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; // Opens a file stream (System.IO.FileStream) to read the file to be uploaded FileStream fs
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();
try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
看起来我应该使用Fileupload控件来从我的网站执行操作,但奇怪的是,该控件创建的是流而不是文件流。是的,我正在FTPing一个文件。这里是一个示例方法,它将我们要针对的两种类型FileInfo和FtpWebRequest作为参数,并在它们之间传输数据。我相信这会奏效
void UploadFileToFtp(FileInfo file, FtpWebRequest req)
{
int buffLength = 2048;
using (var reader = new BinaryReader(file.OpenRead()))
{
using (var writer = new BinaryWriter(req.GetRequestStream()))
{
while (reader.PeekChar() > 0) writer.Write(reader.ReadBytes(buffLength));
}
}
}
希望这有帮助 下面是一个示例方法,它将我们所针对的两种类型FileInfo和FtpWebRequest作为参数,并在它们之间传输数据。我相信这会奏效
void UploadFileToFtp(FileInfo file, FtpWebRequest req)
{
int buffLength = 2048;
using (var reader = new BinaryReader(file.OpenRead()))
{
using (var writer = new BinaryWriter(req.GetRequestStream()))
{
while (reader.PeekChar() > 0) writer.Write(reader.ReadBytes(buffLength));
}
}
}
希望这有帮助 “fileInf”是什么类型的?“reqFTP”是什么类型的呢?我想最终你需要使用一个MemoryStream。但是需要多一点infoFileInfo fileInf=newfileinfofilename;ftpWebRequestReqftp;//从提供的Uri创建FtpWebRequest对象reqFTP=FtpWebRequestFtpWebRequest.Createnew Uriftp://+sFtpId+/+fileInf.Name我传入从fileupload控件获得的文件名。哇,今天早上还没有。我可能不得不使用微软的两步方法,即让控件将其放在服务器上,然后从那里进行FTP?Yuk,将不得不打电话给ISP,看看我是否有空间容纳大文件。目前正在进行的两步过程仍然需要使用100mg文件进行测试。“fileInf”是什么类型的?“reqFTP”是什么类型的呢?我想最终你需要使用一个MemoryStream。但是需要多一点infoFileInfo fileInf=newfileinfofilename;ftpWebRequestReqftp;//从提供的Uri创建FtpWebRequest对象reqFTP=FtpWebRequestFtpWebRequest.Createnew Uriftp://+sFtpId+/+fileInf.Name我传入从fileupload控件获得的文件名。哇,今天早上还没有。我可能不得不使用微软的两步方法,即让控件将其放在服务器上,然后从那里进行FTP?Yuk,将不得不打电话给ISP,看看我是否有空间容纳大文件。目前,执行两步过程仍然需要使用100mg文件进行测试。我会尝试一下,我确实让它执行了两步过程,但我真的希望避免在那里存储这种大小的任何内容。出现错误-输出字符缓冲区太小,无法包含解码字符,编码为“Unicode UTF-8”fallback“System.Text.decodereplacementfallback”。参数名:chars它将文件放在ftp服务器上,但文件已损坏。我通过添加到open.Encoding.ASCII中,消除了错误。但是我正在加载的pdf文件仍然是垃圾。服务器上的文件大小是82KB,pdf应该是613.6KB。我会尝试一下,我确实让它完成了两步过程,但我真的希望避免在那里存储任何这样大小的文件。出错-输出字符缓冲区太小,无法包含解码字符,编码“Unicode UTF-8”回退“System.Text.DecoderReplacementFallback”。参数名:chars它将文件放在ftp服务器上,但文件已损坏。我通过添加到open.Encoding.ASCII中,消除了错误。但是我正在加载的pdf文件仍然是垃圾。服务器上的文件大小是82KB,pdf应该是613.6KB