473,498 Members | 1,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using Filestream - content of the file vanishes

Hi

Probability of this phenomena is like 1/200.
The only code where I am dealing with file is the following:

using (FileStream fs = new FileStream(this.FilePath,
FileMode.Open,FileAccess.Read))
{
try
{
// This is web service call to check if i user has
permissions to upload file
myObj.BeforeUpload(this.FileName,
this.ServerPath);

while ((this.Offset < this.FileSize) && !
this.Stop)
{
fs.Seek(Offset, SeekOrigin.Begin);
buffer = new byte[32 * 1024];
BytesRead = fs.Read(buffer, 0, buffer.Length);

byte[] buff = new byte[BytesRead];

Copy(buffer, 0, buff, 0, BytesRead);

myObj.UploadChunk(this.FileName,
this.ServerPath, this.Offset, buff);

this.Offset += buff.Length;

//report progress to backgroundworker
bgw.ReportProgress((int)(((decimal)Offset /
(decimal)FileSize) * 100));
}
}
finally
{
fs.Close();
}
}
Question is. Why sometimes such an operation leaves my file empty.
This is upload file code. I have left every line unchanged. If you
have more skills than me try to explain this phenomena because I have
no more ideas.
Regards

ps:( I would be really grateful for an explanation )

Piotr Kolodziej

Oct 2 '07 #1
7 1938
Your code cannot truncate the file, unless there is a nice big bug in the
FileStream, but I doubt it.
So the file truncation must come from somewhere else, maybe a sharing or
race condition.
If you change the to

using(FileStream fs=new
FileStream(this.FilePath,FileMode.Open,FileAccess. Read,FileShare.None))

In this way you deny any sharing of the file so no one can truncate it..
unless some fs filter kicks in (like virus scanners).

But this is just a huge guess.

"Piotrekk" <Pi*************@gmail.comha scritto nel messaggio
news:11*********************@19g2000hsx.googlegrou ps.com...
Hi

Probability of this phenomena is like 1/200.
The only code where I am dealing with file is the following:

using (FileStream fs = new FileStream(this.FilePath,
FileMode.Open,FileAccess.Read))
{
try
{
// This is web service call to check if i user has
permissions to upload file
myObj.BeforeUpload(this.FileName,
this.ServerPath);

while ((this.Offset < this.FileSize) && !
this.Stop)
{
fs.Seek(Offset, SeekOrigin.Begin);
buffer = new byte[32 * 1024];
BytesRead = fs.Read(buffer, 0, buffer.Length);

byte[] buff = new byte[BytesRead];

Copy(buffer, 0, buff, 0, BytesRead);

myObj.UploadChunk(this.FileName,
this.ServerPath, this.Offset, buff);

this.Offset += buff.Length;

//report progress to backgroundworker
bgw.ReportProgress((int)(((decimal)Offset /
(decimal)FileSize) * 100));
}
}
finally
{
fs.Close();
}
}
Question is. Why sometimes such an operation leaves my file empty.
This is upload file code. I have left every line unchanged. If you
have more skills than me try to explain this phenomena because I have
no more ideas.
Regards

ps:( I would be really grateful for an explanation )

Piotr Kolodziej
Oct 2 '07 #2
Your code cannot truncate the file, unless there is a nice big bug in the
FileStream, but I doubt it.
So the file truncation must come from somewhere else, maybe a sharing or
race condition.
If you change the to

using(FileStream fs=new
FileStream(this.FilePath,FileMode.Open,FileAccess. Read,FileShare.None))

In this way you deny any sharing of the file so no one can truncate it..
unless some fs filter kicks in (like virus scanners).

But this is just a huge guess.

Thanks for your post. I forgot to say that this weird truncation
happens when I am uploading the same file many times one by one ( to
test reaction of SQL - which is noticing the reception ). I did it for
example 20 times, in one minute, and when i attempt to do it again I
see ( in openFileDialog ) that file is truncated. What do you think
about 'sharing' or 'race condition' now? I know what race condition is
but I have never been thinking about this in file categories.

Oct 2 '07 #3

"Piotrekk" <Pi*************@gmail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
>
>Your code cannot truncate the file, unless there is a nice big bug in the
FileStream, but I doubt it.
So the file truncation must come from somewhere else, maybe a sharing or
race condition.
If you change the to

using(FileStream fs=new
FileStream(this.FilePath,FileMode.Open,FileAccess .Read,FileShare.None))

In this way you deny any sharing of the file so no one can truncate it..
unless some fs filter kicks in (like virus scanners).

But this is just a huge guess.


Thanks for your post. I forgot to say that this weird truncation
happens when I am uploading the same file many times one by one ( to
test reaction of SQL - which is noticing the reception ). I did it for
example 20 times, in one minute, and when i attempt to do it again I
see ( in openFileDialog ) that file is truncated. What do you think
about 'sharing' or 'race condition' now? I know what race condition is
but I have never been thinking about this in file categories.

In any case it isn't the code you've shown, because you open the file
read-only. You're either opening the file somewhere else, or you have disk
corruption and possible hardware failure.
Oct 3 '07 #4
In any case it isn't the code you've shown, because you open the file
read-only. You're either opening the file somewhere else, or you have disk
corruption and possible hardware failure.
Is that possible that the same code is trying to open this file while
the previous thread ( the same code ) hasn't finished with it yet?
This phenomena happens only when I upload the same file saveral times
in a row. Maybe OS is doing something with the opened and closed file
and another upload
form is trying to read it?

No disk corruption occured since this happens also on my new computer.

Oct 5 '07 #5

"Piotrekk" <Pi*************@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
>
>In any case it isn't the code you've shown, because you open the file
read-only. You're either opening the file somewhere else, or you have
disk
corruption and possible hardware failure.

Is that possible that the same code is trying to open this file while
the previous thread ( the same code ) hasn't finished with it yet?
This phenomena happens only when I upload the same file saveral times
in a row. Maybe OS is doing something with the opened and closed file
and another upload
form is trying to read it?

No disk corruption occured since this happens also on my new computer.
Are you catching any exceptions?
Oct 9 '07 #6
On 9 Pa , 16:53, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"Piotrekk" <Piotr.Kolodz...@gmail.comwrote in message

news:11**********************@57g2000hsv.googlegro ups.com...
In any case it isn't the code you've shown, because you open the file
read-only. You're either opening the file somewhere else, or you have
disk
corruption and possible hardware failure.
Is that possible that the same code is trying to open this file while
the previous thread ( the same code ) hasn't finished with it yet?
This phenomena happens only when I upload the same file saveral times
in a row. Maybe OS is doing something with the opened and closed file
and another upload
form is trying to read it?
No disk corruption occured since this happens also on my new computer.

Are you catching any exceptions?
Nope. No exceptions at all. Everything seems to work fine. Files are
uploaded. When I only experiment with one file ( upload ) too
much ...this happens.

Oct 10 '07 #7
Are you catching any exceptions?
Nope. No exceptions at all. Everything seems to work fine. Files are
uploaded. When I only experiment with one file ( upload ) too
much ...this happens.

UploadForm code is available here for those of You who might want to
look at the complete code. It's not long thus it makes it hard to find
such a bug.
http://mion.elka.pw.edu.pl/~pkolodzi/UploadForm.cs

Regards
Piotr Kolodziej

Oct 10 '07 #8

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

Similar topics

1
6037
by: Viorel | last post by:
About FileStream and StreamWriter!? I have a string1 which contains(of course in unicode) a html code with Unicode charset specified in it. 1) bytecontent=...
11
4007
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
7
17252
by: Mark Waser | last post by:
Hi all, I'm trying to post multipart/form-data to a web page but seem to have run into a wall. I'm familiar with RFC 1867 and have done this before (with AOLServer and Tcl) but just can't seem...
9
2124
by: ljlevend | last post by:
I have two questions related to FileStreams. 1. Is there any way to determine whether a file has the permissions that are required by a FileStream constructor? For example, given the following...
3
8204
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
1
4402
by: WeCi2i | last post by:
Okay, I have a problem that has been stumping me for weeks. I have tried many different solutions and this is pretty much my last resort. I have seen a lot of good answers give here so I figured I...
0
4024
by: shlim | last post by:
Currently I'm using VB.Net to perform a http/https multipart form post to a servlet. I'm able to perform the post using HttpWebrequest via GetRequestStream(). However, the servlet returned me with...
13
3964
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
This is a follow-up to my post "Silverlight video doesn't work when file is streamed from handler in ASP.net" at...
0
7125
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
7167
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
7208
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...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.