Solved

How to Read any file using ASP?

Posted on 2009-04-09
37
399 Views
Last Modified: 2013-11-19
How to Read any file using ASP?
Any file here means .doc,.txt,.pdf etc. etc.

or atleast .doc !
0
Comment
Question by:ahsan_tanweer
  • 14
  • 5
  • 4
  • +3
37 Comments
 
LVL 28

Expert Comment

by:sybe
ID: 24106981
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

0
 

Author Comment

by:ahsan_tanweer
ID: 24107115
For .TXT i have the solution.
But i Need to read .doc on priority.
Lookin for a quick and accurate response
0
 
LVL 28

Assisted Solution

by:sybe
sybe earned 33 total points
ID: 24107278
> Lookin for a quick and accurate response

Man, I gave you a quick and accurate response on reading binary and text files. You asked for that, didn't you?
Or do you want to get the text-contents of a Word document? Open it with Application.Word and fake CTRL-A and CTRL-C. But that won't work on a webserver mostly, because a webserver has not Word installed.



0
 

Author Comment

by:ahsan_tanweer
ID: 24113436
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 ?
0
 

Author Comment

by:ahsan_tanweer
ID: 24113443
And want to read the text as well as tables of world document, and web server has world installed in it.
0
 

Author Comment

by:ahsan_tanweer
ID: 24113706
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 .
0
 

Author Comment

by:ahsan_tanweer
ID: 24113730
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
0
 

Author Comment

by:ahsan_tanweer
ID: 24114452
I have asked the question again with more clearity !!
0
 
LVL 28

Expert Comment

by:sybe
ID: 24114484
http://www.4guysfromrolla.com/webtech/110100-1.shtml

You need to use xml.ResponseBody, not xml.ResponseText
0
 

Author Comment

by:ahsan_tanweer
ID: 24115188
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
0
 

Author Comment

by:ahsan_tanweer
ID: 24115231
lets proceed here
0
 
LVL 28

Expert Comment

by:sybe
ID: 24115278
> 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.

0
 
LVL 25

Accepted Solution

by:
kevp75 earned 34 total points
ID: 24125465
asker.  Like already mentioned, you can't do that for a word document.  Most webservers will install MS Office on the server, thus you CANNOT create the object REQUIRED in order to read a word doc through a web page.

The best you can do is open text file (NOT WORD, PDF, ETC...).

Now if these .doc file will always be created in Office 2007, that is a bit of a different story, but then again, those won't be .doc files, they will be .docx files.

So...  it seems you may be SOL, and there is no solution.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24125498
@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?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24126242
I somehow get the impression the author has moved on here (with a whopping 30 points):
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/ASP/Q_24312027.html
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 33 total points
ID: 24127776
If you want to be able to read a Word DOC file then you need to look at a 3rd party component.  The one that seems to be popular and good is OfficeWriter (http://officewriter.softartisans.com/officewriter-372.aspx) .  As mentioned above the server can just do it on its own with just script and the default objects.  To install this or others you will need to have it added to your server.  I mention this because if you are on a hosted server you will need them to allow and do this for you.
bol
0
 

Author Comment

by:ahsan_tanweer
ID: 24128075
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.


0
 

Author Comment

by:ahsan_tanweer
ID: 24128120
b0lsc0tt:I have downloaded the applicatiion Word writer. Now how to use it? can u please explain ?
0
 

Author Comment

by:ahsan_tanweer
ID: 24128704
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

%>
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24130720
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
0
 

Author Comment

by:ahsan_tanweer
ID: 24144640
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.
:)
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24144665
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
0
 

Author Comment

by:ahsan_tanweer
ID: 24154493
@ 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 ?
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24162153
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
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24177050
@ModernMatt
b0lsc0tt is the expert not the author. The author is being a bit awkward here if you read the question and comments correctly.
0
 

Author Comment

by:ahsan_tanweer
ID: 24182635
@ 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. :)
0
 
LVL 25

Expert Comment

by:kevp75
ID: 24185421
@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
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24187028
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>
0
 
LVL 25

Expert Comment

by:kevp75
ID: 24189380
that was a mistake....

lol @ <rant>
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24190417
>>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?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Read about why website design really matters in today's demanding market.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to dynamically set the form action using jQuery.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now