ASP.NET: Delete file from server after TransmitFile command

It’s a common scenario: user needs to download a file from the server by clicking link or a button in your ASPX page. The server-side code for this is pretty straightforward and looks something like this:

01Sub DownloadFile(ByVal i_sServerPath As String, ByVal i_sDisplayName As String)
02   Dim oFile As FileInfo = New FileInfo(i_sServerPath)
03 
04   With Response
05      .Clear()
06      .ClearHeaders()
07      .AddHeader("Content-Length", oFile.Length.ToString())
08      .ContentType = ReturnContentType(oFile.Extension.ToLower())
09      .AddHeader("Content-Disposition", "attachment; filename=" & i_sDisplayName)
10      .TransmitFile(oFile.FullName)
11      .End()
12   End With
13 
14End Sub

The code accepts 2 parameters: i_sServerPath – full path to the file on the server and i_sDisplayName – file name that will be displayed to the user in the “Save As” dialog. Code also populates response headers based on file information. I use this handy function to populate ContentType based on file extention:

01Function ReturnContentType(ByVal i_sfileExtension As String) As String
02   Select Case i_sfileExtension
03      Case ".htm", ".html", ".log" : Return "text/HTML"
04      Case ".txt" : Return "text/plain"
05      Case ".doc" : Return "application/ms-word"
06      Case ".tiff", ".tif" : Return "image/tiff"
07      Case ".asf" : Return "video/x-ms-asf"
08      Case ".avi" : Return "video/avi"
09      Case ".zip" : Return "application/zip"
10      Case ".xls", ".csv" : Return "application/vnd.ms-excel"
11      Case ".gif" : Return "image/gif"
12      Case ".jpg", "jpeg" : Return "image/jpeg"
13      Case ".bmp" : Return "image/bmp"
14      Case ".wav" : Return "audio/wav"
15      Case ".mp3" : Return "audio/mpeg3"
16      Case ".mpg", "mpeg" : Return "video/mpeg"
17      Case ".rtf" : Return "application/rtf"
18      Case ".asp" : Return "text/asp"
19      Case ".pdf" : Return "application/pdf"
20      Case ".fdf" : Return "application/vnd.fdf"
21      Case ".ppt" : Return "application/mspowerpoint"
22      Case ".dwg" : Return "image/vnd.dwg"
23      Case ".msg" : Return "application/msoutlook"
24      Case ".xml", ".sdxl" : Return "application/xml"
25      Case ".xdp" : Return "application/vnd.adobe.xdp+xml"
26      Case Else : Return "application/octet-stream"
27   End Select
28End Function

It’s all good, but what if you need to delete the file from the server immediately after user downloaded it? Continue reading →

Kindle Fire as an Android Tablet

Got my Kindle Fire. I must say – I love everything about this device. It’s fast, powerful, with a very nice screen. There’re gazillion reviews out there, I am not going to repeat them, just mention what I did to make it truly perfect.

Since Fire is powered by Android OS there’s no limit to what can be done with it.

Amazon Kindle Fire as an Android TabletFirst, I rooted it. Mostly I did it so I can install Android Market. Amazon’s own AppStore is nice and all, but very limited comparing to Android Market (even if it has Market’s apps, usually they’re a few versions behind). Beware though – if you root your Fire, you will lose video streaming capabilities from Amazon, so what I did – rooted, installed Market, unrooted (luckily it’s a one-click operation with rooting app).

Second, even though I like Fire’s home screen with the carousel and everything, I am used to more traditional Android display, so I installed ADW Launcher and got 5 home screens, app drawer and all the bells and whistles. And still kept all Fire capabilities (Amazon video, music & books) via pre-installed apps, now available in app drawer.

And last, I installed Dolphin Browser HD to use instead of preinstalled Silk web browser, since Dolphin is much more capable, and superb FBReader to read my own collection of ebooks. Those two apps are permanently docked in my home screen.

I could go on and on, about how you can stream your own music and videos from your home PC via GMote app, how you can now install GMail, YouTube and any other Google app etc. etc. but that would take forever. The conclusion: Fire is an Android tablet and capable of anything Android Tablet can do.

Plants vs. Zombies 2!

Well, not really. But if you’re a fan of the original PvZ and just can’t wait for the sequel – check out this beauty:

“Mini Robot Wars” looks like a straight out clone of Plant vs. Zombies, but as clones go – this one is pretty good, give it a try!

UltraWebMenu: When background doesn’t change on hover in IE9

If you tried to use Infragistics classic UltraWebMenu control in IE9 you may experience issue (even in the latest version, 11.2 at the time of this post) whereby menu items don’t change background on mouse hover even though background is specified in menu’s HoverItemStyle property.

The solution is specify BorderStyle in HoverItemStyle. It can be any value besides NotSet, but the actual attribute has to be there. So for example if you want your hover style to have no borders and your original style looks like:

1<HoverItemStyle
2   ForeColor="White"
3   BackColor="#81C0E9"
4   Height="18px"
5   BorderWidth="0px">
6</HoverItemStyle>

change it to

1<HoverItemStyle
2   ForeColor="White"
3   BackColor="#81C0E9"
4   Height="18px"
5   BorderWidth="0px"
6   BorderStyle="None">
7</HoverItemStyle>

I don’t know why border style affects showing of the background, but there you have it. Adding BorderStyle to HoverItemStyle will enable displaying of background color on hover.

VB for Android

Found this gem today:

Basic 4 Android

It’s a Visual Basic IDE environment for developing Android apps. But unlike other similar solutions it does not require bloated runtime running on the device, Basic4Android easily compiles native APK app.

Don’t learn Java, utilize your existing Visual Basic skills instead. And the community of thousands of developers can be a huge help as well.

Also you’re in luck today. Download the trial, play around with it and if you like it – use discount code “bvqbet” to get 50% off any version! Here’s how:

  1. Visit purchase link: http://www.basic4ppc.com/android/purchase.html
  2. Select Plimus as your checkout option
  3. Enter coupon code bvqbet in the coupon code field
  4. Profit! You get a 50% discound off a regular price

Happy coding!

UltraWebGrid bug: Row is selected on mouse move

Submitted for your approval an UltraWebGrid with CellClickActionDefault=”RowSelect” and SelectTypeRowDefault=”Single” – an ordinary down-to-earth grid. It also posses event handler AfterSelectChangeHandler, also nothing out of the ordinary. But in a minute the aforementioned grid will exhibit properties most unusual. As the alert message ahead reads: Infragistics Bug Continue reading →

ClientScript.RegisterStartupScript: script injected, but not executed

This was one weird mystery. I have used ASP.NET’s method ClientScript.RegisterStartupScript countless times to inject client-side JavaScript into page’s markup from the server-side code and it always worked perfectly. This time I created a very basic page from scratch:

02<head runat="server">
03    <title>My Page</title>
04    <script type="text/javascript" src="script1.js" />
05    <script type="text/javascript" src="script2.js" />
06    <script type="text/javascript" src="script3.js" />
07</head>
08<body>
09    <form id="xfrmMain" runat="server">
10    </form>
11</body>
12</html>

And then injected client-side script into it:

1ClientScript.RegisterStartupScript(Me.GetType, "JSCode", "ProcessData();", True)

where ProcessData() is function from one of the scripts, loaded in the HEAD tag. The script injected just fine, I got a beautiful insert at the bottom of the rendered page:

1<script type="text/javascript">
2//<![CDATA[
3ProcessData();//]]>
4</script>

The problem was – it didn’d do squat – the script did not execute. Why? Continue reading →

How to load IFRAMEs in order according to priority

Let’s say you have a Web page that displays “widgets” a small islands of information. And internally those widgets are IFRAME elements, whose source is loaded dynamically from the same server at runtime in client side JavaScript. So, you have a code similar to this:

1var aIframes = document.getElementsByTagName('IFRAME');
2for (var I = 0; I < aIframes.length; I++) {
3    aIframes[I].src = aIframes[I].getAttribute('originalURL')
4}

This (oversimplified) code assumes that URL for IFRAME source already stored as a custom attribute ‘originalURL’ of IFRAME element (for example placed there by server-side code), but it can equally come from other sources. This approach is usually taken so IFRAMES can initially display a static page with “Please wait. Loading…” animation meanwhile dynamically loading real data – creates a better user experience.

The code loops thru all IFRAMEs setting their SRC attribute, displaying content, so you would see something like

IFRAME widgets

It’s all well and good, but there’s a small problem. SRC of IFRAMEs is assigned in order of their appearance on the page and if URLs are pointing to the same server and one of the earlier IFRAMEs takes a long time to load – it will block the rest of the IFRAMEs from loading. Continue reading →