[英]After read the Stream I cant save file from fileupload
我有一个FileUpload控件,当我单击一个按钮时,我想将FileUpload文件保存到服务器中的文件中。
我检查是否从FileUpload中获取了一个包含数据的文件,但是当我尝试将其保存在服务器中时,创建的文件为空。 我究竟做错了什么?
一开始我有updatepanels,但是现在没有任何更新面板。
string string1, string2, string3;
bool bool1= false, bool2= false, bool3= false;
using (StreamReader reader = new StreamReader(fuSubirPlantilla.PostedFile.InputStream))
{
string contenidoPlantilla = reader.ReadToEnd();
bool1= contenidoPlantilla.Contains("[tag1]");
bool2= contenidoPlantilla.Contains("[tag2]");
bool3= contenidoPlantilla.Contains("[tag3]");
}
if (bool1 && bool2 && bool3)
{
string1 = dropdown1.SelectedValue;
string2 = dropdown2.SelectedValue;
string3 = dropdown3.SelectedValue;
string filename= "filename1_" + string1 + "_" + string2 + "_" + string3 + ".html";
string folder = Server.MapPath( "~/XML_MI/Common/").Replace("/", "\\");
// Me aseguro de que termine en \\
folder = folder.EndsWith("\\") ? folder : folder + "\\";
// Antes de guardar el archivo
string folderFilePath = $"{folder}{filename}";
fuSubirPlantilla.SaveAs(folderFilePath);
//Stream stream = fuSubirPlantilla.PostedFile.InputStream;
//using (FileStream fileStream = File.Create(folderFilePath))
//{
// stream.CopyTo(fileStream);
//}
}
else
{
// Error
}
如您所见,我尝试了另一种方法,但仍然无法正常工作
using (FileStream fileStream = File.Create(folderFilePath))
{
stream.CopyTo(fileStream);
}
该问题与我在开始时阅读所有Stream有关。[ reader.ReadToEnd() ](如果没有,则没有问题)
在块中using
块之后,请尝试移动所有正在执行的代码。 当您退出using
, StreamReader
被处置,它也可能会处置并关闭InputStream
,因此您什么也不会保存到文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.