Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

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 !
0
ahsan_tanweer
Asked:
ahsan_tanweer
  • 14
  • 5
  • 4
  • +3
3 Solutions
 
sybeCommented:
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
 
ahsan_tanweerAuthor Commented:
For .TXT i have the solution.
But i Need to read .doc on priority.
Lookin for a quick and accurate response
0
 
sybeCommented:
> 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ahsan_tanweerAuthor Commented:
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
 
ahsan_tanweerAuthor Commented:
And want to read the text as well as tables of world document, and web server has world installed in it.
0
 
ahsan_tanweerAuthor Commented:
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
 
ahsan_tanweerAuthor Commented:
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
 
ahsan_tanweerAuthor Commented:
I have asked the question again with more clearity !!
0
 
sybeCommented:
http://www.4guysfromrolla.com/webtech/110100-1.shtml

You need to use xml.ResponseBody, not xml.ResponseText
0
 
ahsan_tanweerAuthor Commented:
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
 
ahsan_tanweerAuthor Commented:
lets proceed here
0
 
sybeCommented:
> 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
 
kevp75Commented:
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
 
CodeCruiserCommented:
@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
 
Anthony PerkinsCommented:
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
 
b0lsc0ttCommented:
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
 
ahsan_tanweerAuthor Commented:
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
 
ahsan_tanweerAuthor Commented:
b0lsc0tt:I have downloaded the applicatiion Word writer. Now how to use it? can u please explain ?
0
 
ahsan_tanweerAuthor Commented:
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
 
b0lsc0ttCommented:
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
 
ahsan_tanweerAuthor Commented:
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
 
b0lsc0ttCommented:
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
 
ahsan_tanweerAuthor Commented:
@ 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
 
b0lsc0ttCommented:
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
 
CodeCruiserCommented:
@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
 
ahsan_tanweerAuthor Commented:
@ 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
 
kevp75Commented:
@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
 
b0lsc0ttCommented:
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
 
kevp75Commented:
that was a mistake....

lol @ <rant>
0
 
Anthony PerkinsCommented:
>>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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 14
  • 5
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now