简体   繁体   English

读取流后,我无法从fileupload保存文件

[英]After read the Stream I cant save file from fileupload

I have a FileUpload control, and when I click a button I want to save the FileUpload file to a File in the server. 我有一个FileUpload控件,当我单击一个按钮时,我想将FileUpload文件保存到服务器中的文件中。

I check I am getting a file from the FileUpload with data, but when I try to save it in the server, the file created is empty. 我检查是否从FileUpload中获取了一个包含数据的文件,但是当我尝试将其保存在服务器中时,创建的文件为空。 What Am I doing wrong? 我究竟做错了什么?

I had updatepanels at the beginning, but now there is not any update panel. 一开始我有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
                }

I tried, as you can see, another approach, but it still not working 如您所见,我尝试了另一种方法,但仍然无法正常工作

                    using (FileStream fileStream = File.Create(folderFilePath))
                    {
                        stream.CopyTo(fileStream);
                    }

The problem is related with I read all Stream at the beginning [ reader.ReadToEnd() ](If I dont, I dont have the problem) 该问题与我在开始时阅读所有Stream有关。[ reader.ReadToEnd() ](如果没有,则没有问题)

Please try to move all your code which is now going after using block inside the block. 在块中using块之后,请尝试移动所有正在执行的代码。 When you go out of the using the StreamReader is disposed and it might dispose and close the InputStream as well, thus you save nothing to the file. 当您退出usingStreamReader被处置,它也可能会处置并关闭InputStream ,因此您什么也不会保存到文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM