Link to home
Start Free TrialLog in
Avatar of ahsan_tanweer
ahsan_tanweer

asked on

How to Read any file using ASP?

How to Read any file using ASP?
Any file here means .doc,.txt,.pdf etc. etc.

or atleast .doc !
Avatar of sybe
sybe

It actually depends on the type of file: text or binary. And also xml has it's own object for special handling.

For text-files, use the FileSystem Object. For binary files, use ADODB.Stream.
Function ReadTextFile(ByVal s)
    Dim oFS, oFile, oTextStream
 
    Set oFS = Server.CreateObject("Scripting.FileSystemObject")
    If Not oFS.FileExists(s) Then Exit Function
    Set oFile = oFS.GetFile(s)
    If oFile.Size > 0 Then
        Set oTextStream = oFS.OpenTextFile(s)
        ReadTextFile = oTextStream.ReadAll
        oTextStream.Close: Set oTextStream = Nothing
    End If
    Set oFile = Nothing: Set oFS = Nothing
End Function
 
 
Function ReadBinaryFile(ByVal sFilePath)
    Dim oStream
    Set oStream = Server.CreateObject("ADODB.Stream")
    oStream.Type = 1
    oStream.Open
    oStream.LoadFromFile sFilePath
    ReadBinaryFile = oStream.Read
    oStream.Close
    Set oStream = Nothing
End Function

Open in new window

Avatar of ahsan_tanweer

ASKER

For .TXT i have the solution.
But i Need to read .doc on priority.
Lookin for a quick and accurate response
SOLUTION
Avatar of sybe
sybe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually I am trying to read a world document file using ASP.
below code was reading the file before but all of a sudden its not doin it.
Now its only reading .TXT files.

<%
Response.Buffer = true
Dim objXMLHTTP, xml, text
Set xml = Server.CreateObject  ("Microsoft.XMLHTTP")

xml.Open "GET", "http://localhost/KBS/Stored_Files/File.doc", false
xml.Send
text = xml.ResponseText
Response.write(text)

Set xml = Nothing

%>

so what do you suggest now ?
And want to read the text as well as tables of world document, and web server has world installed in it.
is it possible sybe ?
kindly if you can provide the code as I need to display the world document on the webpage !
World document dosent contain picture but text and tables.
Thanks .
is the below mentioned code  possible?
if yes then it is giving error
{
Server object, ASP 0178 (0x80070005)
The call to Server.CreateObject failed while checking permissions. Access is denied to this object.
}

    Dim oStream,sFilePath
     sFilePath="C:\Inetpub\wwwroot\KBS\Stored_Files\file.doc"
    Set oStream = Server.CreateObject("Word.Application")
    oStream.Type = 1
    oStream.Open
    oStream.LoadFromFile sFilePath
    Response.Write(oStream.Read)
      
    oStream.Close
    Set oStream = Nothing
I have asked the question again with more clearity !!
http://www.4guysfromrolla.com/webtech/110100-1.shtml

You need to use xml.ResponseBody, not xml.ResponseText
Sybe, it didnt work.

code is working but try to understand that I want the exact out put.
now the file which i want to read has the text inside as "Test Output"

but when i run this code the out put which i am getting is "?????" and some invalid characters.

I have tried another code i.e.

dim objWord,objDoc,stxt
Set objWord = CreateObject("Word.Application")

if objWord.FileExists("C:\Inetpub\wwwroot\KBS\Stored_Files\Ahsan.doc") then
Set objDoc = objWord.Documents.Open("C:\Inetpub\wwwroot\KBS\Stored_Files\Ahsan.doc")
Do until objDoc.AtEndofStream
stxt = objDoc.Readline
Response.Write(stxt)
Loop
End if

now this code gives me an error i.e.

Microsoft VBScript runtime (0x800A0046)
Permission denied: 'CreateObject'

kindly suggest in both the cases. thanks
lets proceed here
> but when i run this code the out put which i am getting is "?????" and some invalid characters.

Come one, what do you expect if you read a binary file. A binary file is *not* text, so it is quite unreadable. That is not an error, that is by design. Just try to open a Word document in Notepad.....

> Permission denied: 'CreateObject'

Isn't that clear then? "Permission denied" means exactly what it says. If you want it to work, you have to set permissions for IUSR on the dll you are trying to use with CreateObject.

I think I have given you answers far beyond your original question. Now maybe your question was  not formulated precise enough, but that is something you should think about next time. Please formulate your problem exactly and give us as much information as possible to help you solve your problem.

In this case, your question should have been (as I understand it now) how to extract text and images from a Word document using ASP.

Your have asked for this question to be closed. I am not going to object. Hopefully someone else will try to answer your new question. I consider this one closed.

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Ahsan,
What exactly you are trying to acheive in functionality terms. I know in technical terms that you are trying to open a doc file but why? What would you do with it?
I somehow get the impression the author has moved on here (with a whopping 30 points):
https://www.experts-exchange.com/questions/24312027/Reading-Word-file-from-ASP.html
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry guys I was off for a few days

Answer:
For
Sybe:
I tiried to close this question just to clearify the question.
And Set objWord = CreateObject("Word.Application") is giving the above mentioned error.
although Word is installed on the server. Actually the machine i am working on is the server.

kevp75:
I am not using Office 2007 so the file extension should be .doc.

CodeCruiser:
I am trying to make a portal where if a user wants to make a new page in ASP he could do it without knowing the syntax of ASP. and he would only upload a .doc file and my program will read the file and will display the context of that .doc file in an ASP page.


b0lsc0tt:I have downloaded the applicatiion Word writer. Now how to use it? can u please explain ?
Hi Every One !

Now I am getting following wrror message with below code

Error Type:
Microsoft Word (0x800A1772)
Word could not fire the event.
/KBS/read_file.asp, line 22

<%
dim objWord,objDoc,stxt
Set objWord = CreateObject("Word.Application")

Set objDoc = objWord.Documents.Open("C:\Inetpub\wwwroot\KBS\Stored_Files\Ahsan.doc")

Do until objDoc.AtEndofStream

stxt = objDoc.Readline
Response.Write(stxt)

Loop

objWord.quit
objDoc.close

%>
You need to read the documentation for the install and then look at the code samples.  It should've come with the program but otherwise can be downloaded from their site at http://support.softartisans.com/docs.aspx .  If you have a question about it then let me know what you do find and what part you have a question about.
bol
I have got the solution

<%

Response.Write("")
    set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
    xmlhttp.open "GET","http://localhost/KBS/Stored_Files/Ash.htm", false
    xmlhttp.send ""
    Response.write xmlhttp.responseText
    set xmlhttp = nothing

%>

it is reading tables as well as text for me.
:)
Please clarify how that is working for a Word document.  The "solution" you found will definitely work for the html file you are getting but won't work for a binary file like a Word document.  Since you made a point that it had to read Word documents and the code you provide won't please explain the change or test your solution on a Word document.
I will post this as an objection to stop the auto close process.
bol
@ b0lsc0tt:document file was not readable, so i figured out that if we can save the document file as .html / web page then the code I had "successfully reads the file".
OK ?
Thanks for the response.  Why didn't you close this by accepting the expert comments that said the server/ASP can't do it??  Your "solution" isn't really a solution for what you asked but a way to workaround the limitation of the server.  Experts above provided what you decided to do and helped you get to that result.  It seems very odd that you are ignoring the help they gave so you weren't still trying to read a DOC the server can't read without a 3rd party object/component.
I am going to post this as an objection.  Do not start the auto-close process again without this being completely worked out.  I really see no reason for one of your comments to be accepted as an answer.  There are more appropriate options for closing this.  Please just post a response on how you feel this should be closed (hopefully the above will help you with that) and the moderator can finalize this.
bol
@ModernMatt
b0lsc0tt is the expert not the author. The author is being a bit awkward here if you read the question and comments correctly.
@ b0lsc0tt: I wanted to read a world document file. so for that I found the code which i have already mentioned . it reads the file but with a little modification. All we need is to save the world doc as html and then the code will work fine.
Solution always have an output. :)
@Author...

your point is valid, however that is not what you asked here in this question.  What you asked was how to read a word document, not how to read an html file....

hence the objections
kevp75,
Did you mean to object to the auto-close? Your post was made in a way that did go as an objection. Please clarify if that was a mistake or if not what your objection and recommendation is. It would be REALLY nice to be done with this.
ahsan_tanweer,
What you said in your last comment is fine and great. My problem has always been that you wanted to take credit for it all. The expert responses here made it so you were able to get to the conclusion you made. Because of them you stopped trying to use ASP to read a Word DOC file and decided to do what you said. It is good that you posted the conclusion you made but that isn't what deserves to be the answer here. You got there with the help of experts here. I am still amazed you can't just say "thanks for helping me" and close this giving credit to those that helped. I know you are new here but it would seem after I mentioned this once and objected the first time there would be some change in behavior and responses.
By the way ... please close the duplicate of this question that you opened. That should probably be deleted but you can click Request Attention in that question if you want suggestions or moderator help closing it. Please don't just close that one incorrectly or leave it abandoned.
bol
p.s. <rant>And all of this for a share of 100 points (if any)</rant>
that was a mistake....

lol @ <rant>
>>Please don't just close that one incorrectly or leave it abandoned.<<
And here is the incentive:  I realize your points are limited, but f you request that duplicate question be deleted you will be able to use those refunded 30 points to ask another question!  Don't you think that is a great deal?