Play streamed audio in browser (Asp.net) - asp.net

I am trying to get this to work for quite some time now. I have an asp.net page in which I am trying to play a wav file. The code in the page load event is as follows:
Response.Clear()
Response.ContentType = "audio/wav"
Response.AppendHeader("Content-Disposition", "inline;filename=" + "temp.wav")
Dim filePath As String = Server.MapPath("/temp.wav")
If Not String.IsNullOrEmpty(filePath) Then
'Here I am converting the file to a byte array,as eventually I'll be creating the wav files on the fly
Dim fileBytes As Byte() = GetFileBytes(filePath)
Response.BinaryWrite(fileBytes)
Response.Flush()
End If
The problem I am having is every time I run this page, the windows media player opens up. I would like the audio to be played using some inbuilt plugin in the browser. Something like, when you click on a voice icon, how the sound pops up without opening any player.
If I have the same content in an ashx handler, would it be better?
I could not use the embed tag because, I shall not be having a physical file on the server, it would be generated on the fly using a response stream.
Any help is appreciated!

You'll have to use Flash or ActiveX, or something like that.

Related

Display an image from a Byte Array from SQL into an ASPX page that is currently routed

I have the most convoluted and headache of a way to attempt to load am image on a page. I am using VB.Net, my page is routed from a Routes.XML document, I have a SQL database with images stored as varbinary(MAX) fields, I need to load said images from the page into an control, and everything that i use to use will not work with the routing.
If you need to see code examples let me know, but I was wondering if anyone knew of a way to display an image from bytes in this fashion.
use this code:
Dim Ph As Byte()
Ph = DirectCast(YourImageFromDB), Byte())
Dim img As Image = Nothing
Dim stream = New MemoryStream(Ph)
img = Image.FromStream(stream)
Ended up having a friend show me how he routed with images. What basically happens is that I needed to create an ASHX file to create the image, and put the full path from the image.ashx that I create as the URL I need. Worked like a charm. First time ever working with a handler file so i had no idea how to use it at first.

Let the user download a generated file in ASP.Net

I'm creating an ASP.Net web application and I want to add an export feature. That feature will generate a string (xml, to be specific) when a button is clicked.
I want to enable the user to download this string as a file.
Do I need to create a file on the server's hard drive and open a link to that file or is there a way to download the string directly (so ASP.net takes care of creating and removing the file)?
I am currently tryig to do the following:
Response.Clear();
Response.Buffer = true;
Response.ContentEncoding = Encoding.GetEncoding(1252);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=filename.txls");
//Multiple Response.Write(string) calls to add content to the file
Response.End();
Using firebug, I found out the the user receives the headers, but the file doesn't open.
The Internet Explorer is even able to show me the response text and has a button to save the file (both options are only avilable in the developer tools) and the file is correct, but nontheless, the file still doesn't open a window (nor a in-browser-preview).
When I use the Internet Explorer (which seems to have better Visual Studio debugging integration), a JavaScript error appears:
The EnableScriptGlobalization property cannot be changed during async postbacks or after the Init event.
Calls to Response.Write() seem to be a common cause of this issue. Of course, I call exactly that method multiple times.
I figured out that the JavaScript on the webpage (the script seems to be auto-generated) tries to parse the response, but of course it isn't formatted in the asp.NET internal communication format, but in my own XML format.
How can I tell the page to treat my page as a downloadable file?
string someXML=GetXMLContent();//I assume you have an implementation of this
Response.Clear();
Response.ContentType = "text/xml";
Response.AddHeader("Content-Disposition", "attachment; filename=filename.xml");
Response.Write(someXML);
Response.End();
Couple of things to note. It's probably better to build up your XML string early on, then write it to the response in one shot. It makes for cleaner code, because you can separate your concerns this way.
Also, I switched the content type to "text/xml", because this is not an Excel file. Also, your file extension was incorrect. XML by convention ends in .xml. I removed the content encoding because it's not necessary most of the time. If you're dealing with weird characters feel free to add it back. Also removed the buffer in the name of removing unnecessary things.
Try this, then try opening the resulting file in your text editor and verifying the content.
The problem was that the button responsible for triggering the Excel export was not a PostBackTrigger. As soon as I had changed it, everything worked like a charm.
Nontheless, I don't know hy this fixed everything. What does it change when I set the button as a PostBackTrigger

How to implement a desktop download from web form in VB.NET

In my website that I am currently working on, I store some images in a folder in the same directory as my .aspx files in the project file. What method can I use to write a code so that when someone clicks an asp:Button, the browser downloads the image (with a given url) for them.
Here is a code example of what it would look like.
Protected Sub button_img_Click(sender As Object, e As EventArgs) Handles button_img.Click
MagicalFunctionDownloadImage("image.jpeg")
End Sub
Hope that made sense!
Thanks in advance!
The asp:button will really be just a hyperlink, and you'll set a url item that corresponds to your image. It will point to a handler (*.ashx file) that you build, instead of a page (*.aspx file). The handler works much like a page, but without some of the cruft you won't use, and it will allow to set a few specific headers. You then use Response.BinaryWrite() to send the file data to the browser:
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=<filename here>")
Response.ContentType = "<content/type here>"
Response.BinaryWrite(<path to file or byte array goes here>)
Response.Flush()
Response.End()

Problem displaying PDF in IE8 via Silverlight application

I have a Silverlight 4 application from which I'm attempting to display a PDF. My approach has been to upon a button click in the Silverlight application, use HtmlPage.Window.Navigate to open a new browser window. The URL for that this new browser window navigates to is an ASP.Net web forms page that makes a call to SQL Reporting Services via the SSRS SOAP API. This call returns a byte array which the web form then streams back to the browser with the following code:
byte[] report = SSRSRenderReport(reportPath, primaryId);
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("cache-control", "must-revalidate");
Response.AddHeader("content-length", report.Length.ToString());
Response.Buffer = true;
Response.ContentType = "application/pdf";
this.Response.AddHeader("Content-Disposition", "inline; filename=whatever.pdf");
Response.BinaryWrite(report);
Response.Flush();
Response.End();
This all works quite well when running the application from IE9 and Firefox. However when running the application from IE8, the new browser window is displayed after the button click but then closes immediately without ever displaying the PDF or prompting to open/save the PDF.
If I take Silverlight out of the picture and just browse directly to the URL that renders the report, it works fine, the PDF is displayed in the browser. I've seen a few posts that describe this issue when HTTPS is being used, however I'm only using HTTP currently.
Any suggestions on how to get around this issue would be much appreciated.
Try this, http://www.divelements.com/silverlight/tools.aspx I used this to embed a flash site within a silverlight webpage, so as long as you are sure of the size of the pdf viewer you should be ok.
We discovered that the source of this problem was that we were attempting to display our popup window from a viewmodel rather than a view event handler. Eventually we opted to introduce a new view which contains a hyperlink pointing to the URL of the PDF. This works and was for us, an acceptable solution.

ASP.Net HyperLink and Download directly?

want to have a Hyperlink-Button in a gridView in which I can display a Link for a direct download of files.
So far I am okay, but when I link the file in the NavigateURL, I don't get the file via click, I must right click -> Save as!
That I don't want. Any help?
You could set up an ashx file handler. Your ashx takes the request through the querystring, loads the proper file into memory, sets the proper response headers and streams the file to the browser:
FileInfo fileInfo = new FileInfo(PATH-TO-YOUR-FILE); //You need to specify this
context.Response.ContentType = YOUR-CONTENT-TYPE; //And this
context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
context.Response.WriteFile(fileInfo.FullName);
context.Response.Flush();
context.ApplicationInstance.CompleteRequest()
This lets you have some fine-grain control over the content that is being delivered if you have any concerns about security or maybe keeping track of who has downloaded what file, etc.
This sounds like a browser issue. Possibly the browser is trying to open up some application to handle this and failing. Double-check your application associations and/or try a new browser.
I would add also "Content-Disposition" header to response:
context.Response.AppendHeader("Content-Disposition", "attachment; filename = " + filename);

Resources