How to play media - saved on db as binary - on a website? - asp.net

I have a table on SQL Server 2008, I saved on it videos and audios as binary.
I have to play these media (videos and audios) on my website.
Can I do that?
Is there a way that I could request the media as a link? so I can embed it...
Any help appreciated.
Sorry for my english.

They are probably other ways to do this but i'll just show mine below.
create an asp.net page (image.aspx for instance) that has no content on the page just the #page attributes... see below.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="image.aspx.cs" Inherits="image" %>
You code behind file might look like below:
public void Page_Load(object s, System.EventArgs e)
{
int id = Request.QueryString["image_id"]; //assuming u are getting it from query string
byte[] bt = GetMediaData(id); //image data from database as byte array
Response.Buffer = true;
Response.ContentType = "audio/mp3"; //set mimetype of appropriate media type
Response.BinaryWrite(abt);
Response.Flush();
}
on the page that needs to use the resource:
<asp:Image runat="server" id="Image1" ImageUrl="image.aspx?image_id=xx" />
i'm sure this can be done using http handlers though.

Related

ASP equivalent of Ajax?

Weird question. I need to use a JavaScript array with ID's in it, to fetch additional information from a database, using the ID's as the row ID.
I need to then use this additional information and send it to another file (aspx) using Ajax, which will then use this information to rotate images.
Unless I can use ASP Classic, and ASP.NET (C#) in the same file?
- Or can I use the more or less same ASP code to access my database?
rotate script
<%# Page Language="C#" Debug="true" %>
<%# Import Namespace="System" %>
<%# Import Namespace="System.Drawing" %>
<%# Import Namespace="System.Web" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//string url = Request.QueryString["url"];
string url = #"C:\inetpub\wwwroot\testing\image.jpg";
string rotate_dir = Request.QueryString["dir"];
//create an image object from the image in that path
System.Drawing.Image img = System.Drawing.Image.FromFile(url);
//Rotate the image in memory
if (rotate_dir == "clockwise")
{
//Rotate clockwise
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
} else if (rotate_dir == "anticlockwise")
{
//Rotate anti-clockwise
img.RotateFlip(RotateFlipType.Rotate90FlipXY);
}
//Delete the file so the new image can be saved
System.IO.File.Delete(url);
//save the image to the file
img.Save(url);
//release image file
img.Dispose();
}
</script>
What I use to access my database
'Create connection and load users database
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.ACE.OLEDB.12.0"
conn.Open Server.MapPath("/nightclub_photography/data/database/jamsnaps.mdb")
Hope you understand what I'm trying to do?
As you should know you can freely use Javascript (and thus ajax) in asp-classic.
this means you can easily do the following;
set conn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.Recordset")
conn.open Server.MapPath("foobar path") + ", Microsoft.ACE.OLEDB.12.0"
%><script type="text/javascript">
rotationArray = Array(id_Array.length);
for(int i = 0; i < id_Array.Length; i++
{
<% rs.open("SELECT rotation FROM images WHERE id="+ id_Array[i])%>
rotationArray[i] = <%= rs("rotation") %>;
<%rs.close() %>
}
//send rotationArray via ajax
but in general I would suggest you use Database tools for asp.NET instead.
then you just send your JS-IDArray to the aspx file and do the processing there.
for reference you can check here
You can use C#.Net and ASP together. Its not a very nice way to do it. My understanding is you have to create the C#project first then add any asp pages. This will allow you to call your C# pages from within the asp application.
I havent personally done one of these but I have seen it done for sure so I know its technically possible.

How to solve playing video issue in asp.net

Helli Guys!
I have a issue with playing video based on path of video on run time in web video player. there is a ASP code:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<video src='<%# GetVideoPlayUrl(Eval("id")) %>' width="900" height="400"
controls="" preload=""></video>
</ItemTemplate>
</asp:Repeater>
I have a video Path in database that is saved in db like this ~\StoreVideo\VQOFY.mp4
protected string GetVideoPlayUrl(object Id)
{
int id = Convert.ToInt32(Id);
string ret = "";
ret = "" + VideoUploads.GetVideoPath(id) + "";
return ret; I'm return value `~\StoreVideo\VQOFY.mp4`
}
The video is not uploading in player i also tried this
protected string GetVideoPlayUrl(object Id)
{
int id = Convert.ToInt32(Id);
string ret = "";
ret = "" + VideoUploads.GetVideoPath(id).Replace("~\\","").Replace("\\","/") + "";
return ret; I'm return value `StoreVideo/VQOFY.mp4`
}
still not working i dont know where was im doing wrong I really need a help in this issue. to know what is the mistake I'm doing here. please correct me with my code where was a mistake.hope some buddy have a solution for me with it.
Thank you
URLs that start with ~/ should be 'resolved' before you use them client-side (the <video> element is a client-side HTML element, it is unaware of ASP.NET on the server). Try the following code inside the src attribute:
<%# ResolveUrl(GetVideoPlayUrl(Eval("id"))) %>
If you are returning "~/videos/some-video.mp4" from the GetVideoPlayUrl(id) method, then the call to ResolveUrl() will 'translate' that, maybe to "../../videos/some-video.mp4" or maybe to "videos/some-video.mp4". It all depends on where the current page is located relative to the location of the file.
You should check in the final HTML that the (relative) path from the ASPX page to the video file is now correct.

How to disable ASP.NET render default html tag?

For some reason, i have to make a aspx page to let iphone/android to Post form.
After the data processing of request.form , i have response a JSON string in plain text.
However *.aspx will enforce to render html,head,body tag,so how can i disable it?
I cannot use ashx, because the mobile have to post image via form, as i search the answer by google ,ashx cannot handle the http post files.
Edit:
As SLaks said, ashx can handle POST file using "context.Request.Files" and it works.
You are wrong.
ASHX files can handle HTTP POSTs.
To answer the question, you can delete all of the content in the ASPX file. ASPX files do not need to have any content at all (other than the <%# Page %> directive)
When processing the post, use this code:
Response.Clear();
Response.ContentType = "text/json";
Response.Write("your json goes here");
Response.End();
Clear the Response, set the content-type since it's json, add your json then end it:
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "text/json";
Response.Write("your json");
Response.End();
}
Outputs exactly
your json

problem in sending document when I click cancel

I have a link in aspx page and when i click on it,it shows a popup: open,save,cancel
but when i click cancel on that aspx page no other link works on that page.
code so far:
protected void method1()
{
byte[] byterendered = _Filename.OpenBinary();
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "image/jpeg";
Response.AddHeader("Content-Disposition", "attachment;filename=abc.jpg");
Response.CacheControl = "Public";
Response.BinaryWrite(byterendered);
Response.End();
}
aspx code
<asp:Linkbutton id="link1" runat="server" onClick="method1" Text="LinkA"/>
<asp:Linkbutton id="link2" runat="server" onClick="method2" Text="LinkB" />
As the comments to your question have indicated, the reason is because your Response is being ended after the file dialog shows up. Once the response ends, any other actions on your page will not be registered. I ran into this myself while implementing a download function for my SharePoint app.
Basically, what you want to do is have your link buttons perform a window open script, instead of directly running the file transfer, like the following.
<asp:LinkButton id="link1" runat="server" onClick="window.open('TARGETURL'); return false;" Text="LinkA" />
Replace TARGETURL with an aspx page URL. Then, create a new ASPX page for the URL you specified. It will be pretty much empty, all you need are two lines.
<%# Assembly Name="YOURFOURPARTASSEMBLYSTRINGHERE" %>
<%# Page Language="C#" Inherits"YOURNAMESPACE.DOWNLOADCODE" %>
Replace YOURFOURPARTASSEMBLYSTRINGHERE with, of course, the four-part assembly string for your code. YOURNAMESPACE.DOWNLOADCODE will be replaced with the namespace and class that you will create for the page. The class will need to inherit the base page type, I personally used LayoutsPageBase since that's a perfect thing to use in a SharePoint app. All this class needs is an OnLoad method like the following.
// Don't actually name your class DOWNLOADCODE.
public class DOWNLOADCODE : LayoutsPageBase
{
protected override void OnLoad(EventArgs e)
{
byte[] byterendered = _Filename.OpenBinary(); //More on this afterwards
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "image/jpeg";
Response.AddHeader("Content-Disposition", "attachment;filename=abc.jpg");
Response.CacheControl = "Public";
Response.BinaryWrite(byterendered);
Response.End();
}
}
You will have to retrieve _Filename in this new page, of course. The best way to do this is to take whatever parameters you use to determine _Filename in the first place, and pass it as part of the URL query string.
Using this, clicking the link button will open a new window, but since all this page does it have a file response, it will just open the file dialog and be done with it. Meanwhile, your original aspx page will not have ended its response, so it can continue whatever function you need it to.

How to bind a MemoryStream to asp:image control?

Is there a way to bind a MemoryStream to asp:image control?
Best bet is to create an HttpHandler that would return the image. Then bind the ImageUrl property on the asp:Image to the url of the HttpHandler.
Here is some code.
First create the HttpHandler:
<%# WebHandler Language="C#" Class="ImageHandler" %>
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
public class ImageHandler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
context.Response.Clear();
if (!String.IsNullOrEmpty(context.Request.QueryString["id"]))
{
int id = Int32.Parse(context.Request.QueryString["id"]);
// Now you have the id, do what you want with it, to get the right image
// More than likely, just pass it to the method, that builds the image
Image image = GetImage(id);
// Of course set this to whatever your format is of the image
context.Response.ContentType = "image/jpeg";
// Save the image to the OutputStream
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
else
{
context.Response.ContentType = "text/html";
context.Response.Write("<p>Need a valid id</p>");
}
}
public bool IsReusable
{
get
{
return false;
}
}
private Image GetImage(int id)
{
// Not sure how you are building your MemoryStream
// Once you have it, you just use the Image class to
// create the image from the stream.
MemoryStream stream = new MemoryStream();
return Image.FromStream(stream);
}
}
Next, just call it inside your aspx page where you are using the asp:Image.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="myImage" ImageUrl="~/ImageHandler.ashx?id=1" runat="server" />
</div>
</form>
</body>
</html>
And that is it.
A handler can accept a url parameter like any other request. So instead of linking your <asp:image/> to image.ashx you'd set it to image.ashx?ImageID=[Your image ID here].
I am assuming you need to generate dynamic images from asp.net
You might be in luck
http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16449
Hanselman blogged about it recently
http://www.hanselman.com/blog/ASPNETFuturesGeneratingDynamicImagesWithHttpHandlersGetsEasier.aspx
#Will and Ben Griswald: instead of "image.aspx" use "image.ashx".
It's more light-weight than a full ASP.Net Page, and it's specifically designed to handle content-types other than text/html.
While Databinding a MemoryStream to a Image is not possible, it could be possible to use a Label/GenericControl, some Code and the data URI scheme to embed Images in Pages, but there are severe issues with that approach:
Disadvantages
Embedded content must be extracted and decoded before changes may be made, then re-encoded and re-embedded afterwards.
Cookies are not supported.
Information that is embedded more than once is redownloaded as part of the containing file, and thus does not benefit from the browser's cache.
Browsers may limit URI lengths, creating an effective maximum data size. For example, URIs in previous versions of Opera had limits of 4kB, and 32kB for IE8 Beta 1[citation needed]
Data is included as a simple stream, and many processing environments (such as web browsers) may not support using containers (such as multipart/alternative or message/rfc822) to provide greater complexity such as metadata, data compression, or content negotiation.
Microsoft's Internet Explorer, through version 7 (some 70% of the market as of 2008 Q2), lacks support.
The better Approach is to use a separate "Image.aspx" Page which takes and outputs your MemoryStream, kinda like I did in my Photo Album software that i've created when I started learning ASP.net:
(Don't laugh, that was my first attempt at ASP.net :-)
Edit: Agreed on ASHX, the code above is just to show one sample implementation. When I come around to update the Photo Album, it will use ASHX for that.
You can use Telerik's BinaryImage control for ASP.net.
More info in here:
http://www.telerik.com/products/aspnet-ajax/binaryimage.aspx
Nope.
But you can create a special page to stream that image out. First, you set the URL of the image to the page that performs the streaming, including some url parameters that let you know where to get the image:
<img src="GetImage.aspx?filename=foo" ... />
in GetImage.aspx, you get the filename (or whatever) from the URL, load the image in your MemoryStream, and then write the content of that memory stream directly to the HttpResponse:
response.Expires = 0;
response.Buffer = false;
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.ContentType = "image/jpeg";
response.BinaryWrite(stream);
response.Flush();
response.Close();
For me it was necessary to add "buffer="false" to the #Page. Otherwise I would keep getting the same picture all the time...

Resources