Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Help with Retrieving files code


  • Please log in to reply

#1
Immortalarena

Immortalarena

    Member

  • Member
  • PipPipPip
  • 211 posts
I am working on password protecting a directory so that users have to be logged in to access files. Because of the number of users, which will be in my database on a windows server, I can't add them all to the directory password list. Instead I decided to send the files I want to a piece of code on the site that will get the file and display it for me. I am having some trouble with the code however. I can only get one file type at a time, and Word Documents don't pass through it well at all. I can get PDF Files to prompt for download, but not open on the screen immediately. Other than that this works perfect for my needs. Any help is appreciated. Thanks!

[codebox]<%Option Explicit%>
<meta name=”robots” content=”noindex, nofollow”>

<%
Dim strURL, strUserName, strPassWord
strURL = "http://mysite.com/word.doc"
struserName = "username"
strPassWord = "password"

Dim objXMLHTTP
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
Call objXMLHTTP.Open("GET", strURL, False, strUserName, strPassword)
Call objXMLHTTP.Send()

If objXMLHTTP.Status <> 200 Then
Response.Write(objXMLHTTP.responseText)
Set objXMLHTTP = Nothing
Else
Response.ContentType = "application/msword"
Response.AddHeader "Content-Disposition", "attachment; filename=""word.doc"""
Response.Charset = "UTF-8"
Response.BinaryWrite(objXMLHTTP.responseBody)
Set objXMLHTTP = Nothing
End If
%>[/codebox]
if I change the application/msword to application/pdf and look to a pdf file it works great. Unfortunately I need to be able to access word files as well as Excel Files, Images and PDF's.

Is there a way to make this code universal, or will I have to look at using getpdf, getdoc, getxls, etc. Thanks,

-Matt McCracken

Edited by Immortalarena, 07 October 2009 - 09:31 PM.

  • 0

Advertisements


#2
Immortalarena

Immortalarena

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 211 posts
I updated the Code a little by working with it. It now doesn't force the user to save the PDF files, but opens them in the browser. I still can't get it to work for word and excel files. In that area I need help. Thanks!

New Code:
[codebox]<%Option Explicit%>
<meta name=”robots” content=”noindex, nofollow”>

<%
Dim strURL, strUserName, strPassWord, filename
strURL = "http://mysite.com/uploads/" & Trim(Request.Querystring("file"))
filename = Trim(Request.Querystring("file"))
struserName = "username"
strPassWord = "password"

Dim objXMLHTTP
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
Call objXMLHTTP.Open("GET", strURL, False, strUserName, strPassword)
Call objXMLHTTP.Send()

If objXMLHTTP.Status <> 200 Then
Response.Write(objXMLHTTP.responseText)
Set objXMLHTTP = Nothing
Else
Response.ContentType = "application/pdf"
Response.Charset = "utf-8"
Response.BinaryWrite(objXMLHTTP.responseBody)
Set objXMLHTTP = Nothing
End If
%>[/codebox]
  • 0

#3
Immortalarena

Immortalarena

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 211 posts
When I remove the Meta Tag from the site it works with word files, problem is I don't want google to index this page or links that go to it. Any ideas there?

Final Code without meta Tag:
[codebox]<%Option Explicit%>

<%
Dim strURL, strUserName, strPassWord, filename, ext
strURL = "http://101.mrmatt.net/uploads/" & Trim(Request.Querystring("file")) & Trim(Request.Querystring("ext"))
filename = Trim(Request.Querystring("file"))
ext = Trim(Request.Querystring("ext"))
struserName = "username"
strPassWord = "password"

Dim objXMLHTTP
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
Call objXMLHTTP.Open("GET", strURL, False, strUserName, strPassword)
Call objXMLHTTP.Send()

If objXMLHTTP.Status <> 200 Then
Response.Write(objXMLHTTP.responseText)
Set objXMLHTTP = Nothing
Else
Response.AddHeader "Content-disposition", "attachment; filename=" & filename & ext
if ext = ".txt" then
Response.ContentType = "text/html"
elseif ext = ".pdf" then
Response.ContentType = "application/pdf"
elseif ext = ".doc" then
Response.ContentType = "application/msword"
elseif ext = ".xls" then
Response.ContentType = "application/msexcel"
end if
Response.Charset = "utf-8"
Response.BinaryWrite(objXMLHTTP.responseBody)
Set objXMLHTTP = Nothing
End If
%>[/codebox]
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP