输入输出流在Web窗体功能中不起作用

时间:2018-10-24 10:31:27

标签: io webforms

有人可以告诉我为什么我在此功能上持续读取和写入超时吗?我将其作为即使单击按钮也可以单击的功能背后的代码。数据的一切看起来都很好,直到我到达流部分,它仍然可以逐步执行,但是当我进入该对象后检查Stream对象的内容时,它指出读取超时/写入超时:系统无效的操作异常。

protected void SubmitToDB_Click(object sender, EventArgs e)
        {

            if (FileUploader.HasFile)
            {
                try
                {
                    if (SectionDropDownList.SelectedValue != null)
                    {
                        if (TemplateDropDownList.SelectedValue != null)
                        {
                            // This gets the full file path on the client's machine ie: c:\test\myfile.txt
                            string strFilePath = FileUploader.PostedFile.FileName;
                            //use the System.IO Path.GetFileName method to get specifics about the file without needing to parse the path as a string
                            string strFileName = Path.GetFileName(strFilePath);
                            Int32 intFileSize = FileUploader.PostedFile.ContentLength;
                            string strContentType = FileUploader.PostedFile.ContentType;

                            //Convert the uploaded file to a byte stream to save to your database. This could be a database table field of type Image in SQL Server
                            Stream strmStream = FileUploader.PostedFile.InputStream;
                            Int32 intFileLength = (Int32)strmStream.Length;
                            byte[] bytUpfile = new byte[intFileLength + 1];
                            strmStream.Read(bytUpfile, 0, intFileLength);
                            strmStream.Close();





                            saveFileToDb(strFileName, intFileSize, strContentType, bytUpfile); // or use FileUploader.SaveAs(Server.MapPath(".") + "filename") to save to the server's filesystem.
                            lblUploadResult.Text = "Upload Success. File was uploaded and saved to the database.";
                        }
                    }
                }
                catch (Exception err)
                {
                    lblUploadResult.Text = "The file was not updloaded because the following error happened: " + err.ToString();
                }
            }
            else
            {
                lblUploadResult.Text = "No File Uploaded because none was selected.";
            }
        }

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

using (var fileStream = FileUploader.PostedFile.InputStream)
{
    using (var reader = new BinaryReader(fileStream))
    {
        byte[] bytUpfile = reader.ReadBytes((Int32)fileStream.Length);
        // SAVE TO DB...
    }
}