C# - How To Safely Read From A Stream
Nov 11, 2010
byte[] bytes = new byte[uploader.UploadedFiles[0].InputStream.Length];
uploader.UploadedFiles[0].InputStream.Read(bytes, 0, bytes.Length);
var storedFile = new document();
string strFullPath = uploader.UploadedFiles[0].FileName;
string strFileName = Path.GetFileName(strFullPath);
storedFile.document_id = Guid.NewGuid();
storedFile.content_type = uploader.UploadedFiles[0].ContentType;
storedFile.original_name = strFileName;
storedFile.file_data = bytes;
storedFile.date_created = DateTime.Now;
db.documents.InsertOnSubmit(storedFile);
db.SubmitChanges();
If:
Reading from a stream in a single call to Read is very dangerous. You're assuming all the data will be made available immediately, which isn't always the case. You should always loop round, reading until there's no more data.
How should I change the above code to make it 'less dangerous'?
View 1 Replies
Similar Messages:
Jul 19, 2010
i will be passing the xml file like this:
[code]....
error:
Error 1 The best overloaded method match for 'System.Xml.Linq.XDocument.Load(string)' has some invalid arguments
cannot convert from 'System.IO.Stream' to 'string'
View 2 Replies
Aug 31, 2010
We have a process that calles a url to pull an image (in this case a .tif image) and it calls this function which works most of the time but recently is getting errors. If I step through the While loop of a 92k file it gets to the end and then errors on the following line:
nBytesRead = stStream.Read(baBytes, 0, MAX_BUF)
I have added a Try/End Try statement which appears to work but is there a better option?
The error I get is:
System.IO.IOException: Unable to read data from the transport connection: The connection was closed.
Code:
Private Function ReadAsByteArray(ByVal stStream As Stream) As Byte()
Dim msBuffer As New MemoryStream()
Dim swWriter As New StreamWriter(msBuffer)
Dim nTotalBytesRead As Integer
Dim nBytesRead As Integer
Dim baBytes(MAX_BUF) As Byte
nTotalBytesRead = 0
nBytesRead = MAX_BUF
While True
'ORIGINALLY THE TRY IS NOT THERE
Try
nBytesRead = stStream.Read(baBytes, 0, MAX_BUF)......
View 8 Replies
Feb 22, 2010
I would like to read all content that's been written to the output stream. I'm attempting to do this using an HTTP module, and it seems like the obvious timing would be when handling the PreSendRequestContent event.
However, if the output stream seems to be set to write-only, as I can't read using a StreamReader. Is there a way I read and re-write the content without writing my own IIS module?
View 1 Replies
Jan 10, 2011
My web page contains a file upload control that is used to select excel file. What my code does; is to read the content of that file and store its content to DB. Right now, I save this file to server and read its content using OLEDB.I want to know is there any way i can read the content without saving it on the server?
View 2 Replies
Aug 30, 2010
i am trying to read file from a stream.
and i am using stream.read method to read the bytes. So the code goes like below
FileByteStream.Read(buffer, 0, outputMessage.FileByteStream.Length)
Now the above gives me error because the last parameter "outputMessage.FileByteStream.Length" returns a long type value but the method expects an integer type.
View 1 Replies
Feb 22, 2011
I have an excel file in my Response Output stream. I can Open the stream as a file after a prompt, but it doesn't seem I can save it directly to a specified folder on my client.
View 1 Replies
Jul 30, 2010
Will this cause any harm? Does it serve any purpose other than tell browsers you have .net installed?I like this article about changing the header to Pure Evil.[URL]
View 3 Replies
Feb 16, 2011
is there a way to safely keep the value of a password field during postbacks in ASP.NET?
I was thinking of the viewstate, but I don't want to print it clearly in the HTML code by setting the control value equals to the viewstate content at every postback.
View 3 Replies
Jun 10, 2010
I have a web service asp.net project which has a global.asax with empty Application_Start and Application_End implementations.As far as I can understand, in this case it is of no use and could be removed (correct me if I'm wrong).Do I need to do anything other than delete global.asax and global.asax.cs (such as change something in web.config or in the project settings)?Just asking in order to not screw up some dependencies I'm not aware of...
View 1 Replies
Mar 3, 2010
I have rendered one of my controls into a string. I want to safely split the html string. I don't want any hanging html tags. I am working on a pagination control adapter.How can I split my string, around less than a set number of chars) safely taking HTML into account?
View 2 Replies
Oct 22, 2010
I'm working with dynamic fields in ASP.NET due to a very specifc and rigid end-user requirement that would take 2 hours just to explain. Suffice it to say, I can't make the requirement go away.
Anyway, I have a working solution in place; no problems with controls loading, rendering or maintaining their ViewState. This is what my OnLoad looks like:
[code]....
View 1 Replies
Jul 21, 2010
Read Receipt And Read Outlook Inbox for the same
View 2 Replies
Jun 29, 2010
1) I notice that the module gets it's data in chunks. This is problematic for me because I'm using a regex to find and replace. If I get a partial match in one chunk and the rest of the match in the second, it will not work. Is there any way to get the entire response before I do my thing to it? I have seen code where it appends data to a string builder until it uses a matches on an "" end tag but my code must work for more that just (xml, custom tags, etc). I don't know how to detect the End Of Stream or if that is even possible.
View 1 Replies
Dec 20, 2010
Assume we have the access to sth.avi on [URL] with a direct link.
e.g : [URL] Also we have a website => www.dl2.com.
Now, we wanna let some users to download sth.avi from [URL], but we don't have enough space on [URL] to save sth.avi. Is it possible in ASP.NET for us to read sth.avi from www.dl.com as streaming file and share it without saving the file on our domain.
P.S: I don't wanna share this link [URL], actually I wanna share something like this link [URL]
View 3 Replies
Nov 10, 2010
I'm trying to return an xml string from a IHttpHandler to a like this: context.Response.Write(xml); When I receive the response in my .aspx.cs I try to load the document as follows: var xml = XDocument.Load(xmlString); but I get an Illegal Characters in Path error message. I've also tried
context.Response.Write(context.Server.HtmlEncode(xml));
and
var xml = XDocument.Load(Server.HtmlDecode(xmlString));
but I get the same message. Is there any way I can return XML from my IHttpHandler?
View 3 Replies
Aug 29, 2010
I have made a WCF web service that streams a 200 mb zip file. how i can read this stream at my client and then give option to the user to download that
View 1 Replies
Dec 16, 2010
I am looking to add video stream into my web application for webcams.owever, I have never used the .NET framework to do so. Any ideas were I should start?
View 9 Replies
Jun 18, 2010
I have a working solution on an IIS6, which I made using this guide: FLV Flash video streaming with ASP.NET 2.0, IIS and HTTP handler
The app is basically a FLV Player, but streaming is essential to it.
Now, one of our customers is running IIS7 and the streaming doesn't work.
View 2 Replies
Jan 18, 2010
I have a website built in ASP.NET 2.0 and a Windows Server 2003 server streaming live videos with Windows Media Services.
To play the content i usually embed a Windows Media Player in my page and make it open the publication URL (let's say http://my.server.com/live), and it works fine.
The problem is that when no stream is playing, an empty stopped player appears and i don't like that
So i'd like to check in my (VB) code if there's actually something being streamed BEFORE i open the player, and display a 'no active stream right now, please try later' message instead.
View 5 Replies
Dec 9, 2010
I want to store .flv files in the database and not in the file system.
This is what I can do right now: Successfully convert .wmv and .mpeg to .flv with ffmpeg. Store images in SQL Server and show them on my page with an httphandler. Same with .avi and .mpeg videos. (It's up to the user's software if he can view it though) Play .flv files in the browser if the file is located in the file system and not in the database.
What I can't do is: Stream .flv videos to JW Player directly from the database. (Stored as binary data)
I've searched the internet for two days now but I can't get it to work. It feels as if I'm almost there though. The JW Player opens up and starts to "buffer", but nothing happens.
I know there's no easy answer but if anyone has done this before, or something similar, I'd like to know how you did. I feel I've got too much code to post it all here.
View 3 Replies
Feb 19, 2011
public void ZipExtract(Stream inputStream, string outputDirectory)
{
using (ZipFile zip = ZipFile.Read(inputStream))
{
Directory.CreateDirectory(outputDirectory);
zip.ExtractSelectedEntries("name=*.jpg,*.jpeg,*.png,*.gif,*.bmp", " ", outputDirectory,
ExtractExistingFileAction.OverwriteSilently);
[Code]....
I try to read zip archive from stream and extract files. Got the following exception in the line "using (ZipFile zip = ZipFile.Read(inputStream))" : ZipEntry::ReadDirEntry(): Bad signature (0xC618F879) at position 0x0000EE19. how to handle this exception?
View 2 Replies
Dec 9, 2010
Has anybody been able to stream .flv videos from a database to a flash player?
I currently store videos (.avi, .mpeg, .wmv) and images in my database and I can stream these pretty good. I'm using a httphandler to do this.
But I can't get .flv videos to stream to the browser. If I don't upload the .flv file to the database but store it in the filesystem, everything works great.
Is it even possible to send a byte stream from the database to the flashplayer in the browser? I think it could have something to do with the headers of the .flv?
Can I do this or should I keep the videos in the file system?
View 3 Replies
Nov 2, 2010
How do I make it so any XML stream can be returned from a Controller Action without causing an error on the browser - Internet Explorer cannot display this feed?
When I return a ContentResult ( ContentType = "text/xml" ) that contains the xml elements <FeedSubmissionInfo>, <FeedSubmissionId> or <FeedType> I get an error in the browser - Internet Explorer cannot display this feed.
Currently, I do a String.Replace( "Feed", "Xeed" ) to get the XML to display in the browser.
public ContentResult InventoryFeed()
{
ContentResult cr = new ContentResult();
cr.Content = System.IO.File.ReadAllText("c:\downloads\amazon\response.txt");
cr.Content = cr.Content.Replace("Feed", "Xeed") ;
[Code]....
View 2 Replies
Nov 25, 2010
How do I stream my webcam through a webpage in c# using asp.net
View 3 Replies