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

Download.asp
<%@Language="VBScript"%>
<%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.Clear
Response.Write("No file specified.")
Response.End
ElseIf InStr(strPath, "..") > 0 Then
Response.Clear
Response.Write("Illegal folder location.")
Response.End
ElseIf Len(strPath) > 1024 Then
Response.Clear
Response.Write("Folder path too long.")
Response.End
Else
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
Response.Clear
'-- 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")
objStream.Open
'-- set as binary
objStream.Type = 1
Response.CharSet = "UTF-8"
'-- load into the stream the file
objStream.LoadFromFile(strAbsFile)
'-- send the stream in the response
Response.BinaryWrite(objStream.Read)
objStream.Close
Set objStream = Nothing
Set objFile = Nothing
Else 'objFSO.FileExists(strAbsFile)
Response.Clear
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 + "//" + window.location.host + "/Download.asp?file=" + fileURL;  
window.open(url,'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:
download.asp

Reference:
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