Monday, January 25, 2010

Download a file from URL in CRM text box

Sometimes you may want to download a file from URL showing in CRM text box. By default, when you click the URL, CRM will show content of the file in a new window (if it is a known MIME type). You can click 'Save As' from File menu to save the file. But, it is not very convenient for user to go through the whole process.  

Here is the solution:

  1. Create an ASP page (download.asp) and place it under ISV folder
  2. Attach Click Event on Text Box to download a file

<%Option Explicit%>
<%Response.Buffer = True%>

On Error Resume Next

Dim strPath
strPath = CStr(Request.QueryString("file"))

'-- do some basic error checking for the QueryString
If strPath = "" Then
Response.Write("No file specified.")
ElseIf InStr(strPath, "..") > 0 Then
Response.Write("Illegal folder location.")
ElseIf Len(strPath) > 1024 Then
Response.Write("Folder path too long.")
Call DownloadFile(strPath)
End If

Private Sub DownloadFile(file)
'-- Detect HTTP/HTTPS
Dim objRegExp
Set objRegExp = new RegExp
objRegExp.Pattern = "^h?[t|f]tps?://"
objRegExp.IgnoreCase = True
objRegExp.Global = True 
If objRegExp.Test(file) = True Then
objRegExp.Pattern = "([A-Za-z]{3,9})://([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((/[-\+~%/\.\w]+)?\??([-\+=&;%@\.\w]+)?#?([\w]+)?)?"
Dim matches
Set matches = objRegExp.Execute(file)
file = matches(0).SubMatches(5)
End If
'set absolute file location
Dim strAbsFile
strAbsFile = Server.MapPath(file)
'-- create FSO object to check if file exists and get properties
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'-- check to see if the file exists
If objFSO.FileExists(strAbsFile) Then
Dim objFile
Set objFile = objFSO.GetFile(strAbsFile)
'-- first clear the response, and then set the appropriate headers
'-- the filename you give it will be the one that is shown
' to the users by default when they save
Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name
Response.AddHeader "Content-Length", objFile.Size
Response.ContentType = "application/octet-stream"
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
'-- set as binary
objStream.Type = 1
Response.CharSet = "UTF-8"
'-- load into the stream the file
'-- send the stream in the response
Set objStream = Nothing
Set objFile = Nothing
Else 'objFSO.FileExists(strAbsFile)
Response.Write("File not found!")
End If
Set objFSO = Nothing
End Sub

Attach On Click Event (place under Form_OnLoad event)
DownloadFile = function(fileURL)
var url= window.location.protocol + "//" + + "/Download.asp?file=" + fileURL;,'popup','width=100,height=50,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=100,top=200');
crmForm.all.[attribute_name].attachEvent('onclick', DownloadFile);

Syntax Example: 
1. Download.asp?file=/SampleFiles/test.txt
2. Download.asp?file=http://localhost/SampleFiles/test.txt

Download Link:

Downloading any file using ASP, FSO and the ADODB Stream object

1 comment:

Muhammad Sohail said...

i had the problem (path too long) as in code above, and then i used "Long Path Tool" which solved my problem easily and quickly.

Post a Comment