473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problems storing upload file in session (ObjectDisposedException)

Hi

I have the following code. I upload an XML file using the FileUpload object,
store the stream in a session so the user gets the chance to confirm some
options then pass the stream from the Session to an XmlReader.

if (performImport == false)
{
Session["__xmlImportFile"] = fileUpload1.FileContent;
//... some other code
}
else
{
if (Session["__xmlImportFile"] != null)
{
Stream xmlFile = (Stream)Session["__xmlImportFile"];
xmlFile.Position = 0;
// ... pass xmlFile to XmlReader
}
Session.Remove("__xmlImportFile");
}

This works fine on the Visual Studio web server. When I transfer the code to
an IIS web server I can only get the code to work if the uploaded file is
80Kb or less. Anything over that and I get an ObjectDisposedException
(Cannot access a closed file.) The size in bytes of the stream from the
session appears to be correct before the XmlReader is called.

[ObjectDisposedException: Cannot access a closed file.]
System.IO.__Error.FileNotOpen() +56
System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin) +1945963
System.Web.TempFile.GetBytes(Int32 offset, Int32 length, Byte[] buffer,
Int32 bufferOffset) +35
System.Web.HttpRawUploadedContent.CopyBytes(Int32 offset, Byte[] buffer,
Int32 bufferOffset, Int32 length) +112
System.Web.HttpInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)
+47
System.Xml.XmlTextReaderImpl.InitStreamInput(Uri baseUri, String
baseUriStr, Stream stream, Byte[] bytes, Int32 byteCount, Encoding encoding)
+196
System.Xml.XmlTextReaderImpl..ctor(Stream stream, Byte[] bytes, Int32
byteCount, XmlReaderSettings settings, Uri baseUri, String baseUriStr,
XmlParserContext context, Boolean closeInput) +62
System.Xml.XmlReader.CreateReaderImpl(Stream input, XmlReaderSettings
settings, Uri baseUri, String baseUriStr, XmlParserContext inputContext,
Boolean closeInput) +68
System.Xml.XmlReader.Create(Stream input, XmlReaderSettings settings,
String baseUri) +37
System.Xml.XmlReader.Create(Stream input, XmlReaderSettings settings) +30

Can you tell me what is happening here and how I might resolve it?

Many thanks
Andrew
Jul 19 '07 #1
6 17084
J055 wrote:
Hi

I have the following code. I upload an XML file using the FileUpload object,
store the stream in a session so the user gets the chance to confirm some
options then pass the stream from the Session to an XmlReader.

if (performImport == false)
{
Session["__xmlImportFile"] = fileUpload1.FileContent;
As you haven't created the stream yourself, you don't have control over
when the stream is disposed. When the FileUpload control is disposed, it
also disposes the stream.

If you need to store something in a session variable, you have to store
the actual content of the file, not the stream used to read it.

Generally it's a bad idea to store large objects in session variables,
though. It uses up memory, and that is a limited resource on the server.
You should consider other options for storing it.
//... some other code
}
else
{
if (Session["__xmlImportFile"] != null)
{
Stream xmlFile = (Stream)Session["__xmlImportFile"];
xmlFile.Position = 0;
// ... pass xmlFile to XmlReader
}
Session.Remove("__xmlImportFile");
}

This works fine on the Visual Studio web server.
No, not fine. You just got lucky (?) that it didn't blow up.
When I transfer the code to
an IIS web server I can only get the code to work if the uploaded file is
80Kb or less. Anything over that and I get an ObjectDisposedException
(Cannot access a closed file.) The size in bytes of the stream from the
session appears to be correct before the XmlReader is called.

[ObjectDisposedException: Cannot access a closed file.]
System.IO.__Error.FileNotOpen() +56
System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin) +1945963
System.Web.TempFile.GetBytes(Int32 offset, Int32 length, Byte[] buffer,
Int32 bufferOffset) +35
System.Web.HttpRawUploadedContent.CopyBytes(Int32 offset, Byte[] buffer,
Int32 bufferOffset, Int32 length) +112
System.Web.HttpInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)
+47
System.Xml.XmlTextReaderImpl.InitStreamInput(Uri baseUri, String
baseUriStr, Stream stream, Byte[] bytes, Int32 byteCount, Encoding encoding)
+196
System.Xml.XmlTextReaderImpl..ctor(Stream stream, Byte[] bytes, Int32
byteCount, XmlReaderSettings settings, Uri baseUri, String baseUriStr,
XmlParserContext context, Boolean closeInput) +62
System.Xml.XmlReader.CreateReaderImpl(Stream input, XmlReaderSettings
settings, Uri baseUri, String baseUriStr, XmlParserContext inputContext,
Boolean closeInput) +68
System.Xml.XmlReader.Create(Stream input, XmlReaderSettings settings,
String baseUri) +37
System.Xml.XmlReader.Create(Stream input, XmlReaderSettings settings) +30

Can you tell me what is happening here and how I might resolve it?

Many thanks
Andrew
--
Göran Andersson
_____
http://www.guffa.com
Jul 19 '07 #2
As Goran indicated, you might want to try this approach, which stores the
contents (that's what you want anyway, right?):

Int32 fileLen;
// Get the length of the file.
fileLen = FileUpload1.PostedFile.ContentLength;
// Create a byte array to hold the contents of the file.
Byte[] Input = new Byte[fileLen];
// Initialize the stream to read the uploaded file.
myStream = FileUpload1.FileContent;
// Read the file into the byte array.
myStream.Read(Input, 0, fileLen);
// Store the byte array in Session
Session["__xmlImportFileBytes"] = Input;

--Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com

"J055" wrote:
Hi

I have the following code. I upload an XML file using the FileUpload object,
store the stream in a session so the user gets the chance to confirm some
options then pass the stream from the Session to an XmlReader.

if (performImport == false)
{
Session["__xmlImportFile"] = fileUpload1.FileContent;
//... some other code
}
else
{
if (Session["__xmlImportFile"] != null)
{
Stream xmlFile = (Stream)Session["__xmlImportFile"];
xmlFile.Position = 0;
// ... pass xmlFile to XmlReader
}
Session.Remove("__xmlImportFile");
}

This works fine on the Visual Studio web server. When I transfer the code to
an IIS web server I can only get the code to work if the uploaded file is
80Kb or less. Anything over that and I get an ObjectDisposedException
(Cannot access a closed file.) The size in bytes of the stream from the
session appears to be correct before the XmlReader is called.

[ObjectDisposedException: Cannot access a closed file.]
System.IO.__Error.FileNotOpen() +56
System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin) +1945963
System.Web.TempFile.GetBytes(Int32 offset, Int32 length, Byte[] buffer,
Int32 bufferOffset) +35
System.Web.HttpRawUploadedContent.CopyBytes(Int32 offset, Byte[] buffer,
Int32 bufferOffset, Int32 length) +112
System.Web.HttpInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)
+47
System.Xml.XmlTextReaderImpl.InitStreamInput(Uri baseUri, String
baseUriStr, Stream stream, Byte[] bytes, Int32 byteCount, Encoding encoding)
+196
System.Xml.XmlTextReaderImpl..ctor(Stream stream, Byte[] bytes, Int32
byteCount, XmlReaderSettings settings, Uri baseUri, String baseUriStr,
XmlParserContext context, Boolean closeInput) +62
System.Xml.XmlReader.CreateReaderImpl(Stream input, XmlReaderSettings
settings, Uri baseUri, String baseUriStr, XmlParserContext inputContext,
Boolean closeInput) +68
System.Xml.XmlReader.Create(Stream input, XmlReaderSettings settings,
String baseUri) +37
System.Xml.XmlReader.Create(Stream input, XmlReaderSettings settings) +30

Can you tell me what is happening here and how I might resolve it?

Many thanks
Andrew
Jul 19 '07 #3
when the request completes, the stream is closed, so storing in session
is useless. if small, you could read the stream into sring, or into a
dom and store in session

-- bruce (sqlwork.com)

J055 wrote:
Hi

I have the following code. I upload an XML file using the FileUpload object,
store the stream in a session so the user gets the chance to confirm some
options then pass the stream from the Session to an XmlReader.

if (performImport == false)
{
Session["__xmlImportFile"] = fileUpload1.FileContent;
//... some other code
}
else
{
if (Session["__xmlImportFile"] != null)
{
Stream xmlFile = (Stream)Session["__xmlImportFile"];
xmlFile.Position = 0;
// ... pass xmlFile to XmlReader
}
Session.Remove("__xmlImportFile");
}

This works fine on the Visual Studio web server. When I transfer the code to
an IIS web server I can only get the code to work if the uploaded file is
80Kb or less. Anything over that and I get an ObjectDisposedException
(Cannot access a closed file.) The size in bytes of the stream from the
session appears to be correct before the XmlReader is called.

[ObjectDisposedException: Cannot access a closed file.]
System.IO.__Error.FileNotOpen() +56
System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin) +1945963
System.Web.TempFile.GetBytes(Int32 offset, Int32 length, Byte[] buffer,
Int32 bufferOffset) +35
System.Web.HttpRawUploadedContent.CopyBytes(Int32 offset, Byte[] buffer,
Int32 bufferOffset, Int32 length) +112
System.Web.HttpInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)
+47
System.Xml.XmlTextReaderImpl.InitStreamInput(Uri baseUri, String
baseUriStr, Stream stream, Byte[] bytes, Int32 byteCount, Encoding encoding)
+196
System.Xml.XmlTextReaderImpl..ctor(Stream stream, Byte[] bytes, Int32
byteCount, XmlReaderSettings settings, Uri baseUri, String baseUriStr,
XmlParserContext context, Boolean closeInput) +62
System.Xml.XmlReader.CreateReaderImpl(Stream input, XmlReaderSettings
settings, Uri baseUri, String baseUriStr, XmlParserContext inputContext,
Boolean closeInput) +68
System.Xml.XmlReader.Create(Stream input, XmlReaderSettings settings,
String baseUri) +37
System.Xml.XmlReader.Create(Stream input, XmlReaderSettings settings) +30

Can you tell me what is happening here and how I might resolve it?

Many thanks
Andrew

Jul 19 '07 #4
Peter Bromberg [C# MVP] wrote:
As Goran indicated, you might want to try this approach, which stores the
contents (that's what you want anyway, right?):

Int32 fileLen;
// Get the length of the file.
fileLen = FileUpload1.PostedFile.ContentLength;
// Create a byte array to hold the contents of the file.
Byte[] Input = new Byte[fileLen];
// Initialize the stream to read the uploaded file.
myStream = FileUpload1.FileContent;
// Read the file into the byte array.
myStream.Read(Input, 0, fileLen);
Oops! ;)

The Read method returns the actual number of bytes read, which may be
less than the number of bytes requested. You have to loop until you have
read all the bytes, i.e. until Read returns zero.
// Store the byte array in Session
Session["__xmlImportFileBytes"] = Input;

--Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com
--
Göran Andersson
_____
http://www.guffa.com
Jul 19 '07 #5
Hi Andrew,

As other members have mentioned, storing the Stream object directly in
Session is not a safe approach as you can not predict or confirm when the
stream will be closed or disposed. And the behavior vary much between VS
2005 web test server and IIS hosting context. For your scenario, you can
consider the following approachs:

1. Read out the actual binary content(byte array) from the stream and store
the binary content into session state. However, as others have said, this
is not quite a good way since it will cause session state memory
pressure(when there is many large files stored at the same time).

2. You can consider save uploaded file into a temp dir(in a temp file). And
in the session state, you simply store the path and file name of the temp
file.

How do you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.


Jul 20 '07 #6
Hi

Thanks to everyone for there assistance. It makes sense to me now about the
stream being disposed of outside my control. The reason I thought it OK to
store a stream in a Session is simply because it's is a very occasional
demand on the system for our application.

I've decided to store the file on disk, however. I found the
Path.GetTempFileName method very useful for this.

Many thanks
Andrew
"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:oT**************@TK2MSFTNGHUB02.phx.gbl...
Hi Andrew,

As other members have mentioned, storing the Stream object directly in
Session is not a safe approach as you can not predict or confirm when the
stream will be closed or disposed. And the behavior vary much between VS
2005 web test server and IIS hosting context. For your scenario, you can
consider the following approachs:

1. Read out the actual binary content(byte array) from the stream and
store
the binary content into session state. However, as others have said, this
is not quite a good way since it will cause session state memory
pressure(when there is many large files stored at the same time).

2. You can consider save uploaded file into a temp dir(in a temp file).
And
in the session state, you simply store the path and file name of the temp
file.

How do you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no
rights.


Jul 23 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Muppy | last post by:
I've created a page with a form to upload files: <h1>Upload di un file</h1> <form enctype="multipart/form-data" method="post" action="do_upload1.php"> <p><strong>File da trasferire:</strong><br>...
5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
1
by: Selena | last post by:
I have vb.net windows application which need to upload file to server. It can run properly without using VPN. How can I run it with VPN? Is there any components or code to upload file? Or is there...
9
by: david | last post by:
I have a class with some business-logic and with every roundtrip, I need an instance of this class, so I have to create it, every time again. That doesn't seem very efficient. I thought it would...
4
by: bienwell | last post by:
Hi all, I developed an web page in ASP.NET to upload file into the server. In the Web.config file, I declared <httpRuntime executionTimeout="1200" maxRequestLength="400000" /> The MAX...
2
by: suresh_nsnguys | last post by:
Hi, I am working in digital signage application where user can upload image,flash and movie files .and later thay can view the uploaded files in digital LCD screen. I am facing 1...
4
by: google.com | last post by:
Hi there! I've been digging around looking for a sample on how to upload a file without user action. I found the following article covering the area: ...
1
by: Sam | last post by:
Hi all, We have an old web application written in java and we want to convert this application to .net. this app stores straight html content forms in our database. These forms then get loaded...
2
by: lka527 | last post by:
I am trying to make a site available for someone to *upload* certain type of text file (.txt) that can be parsed by written code. the upload file is available on the main site but I need this to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.