I put together the following to parse the xml. I was thinking that by checking
the Status Code and making sure it was 200 that that would indicate the
download was successful. Am I correct in assuming this?
It’s a pretty good bet. All HTTP servers should respond a 200 message when
returning the requested data, and some other code when not. For the most part,
this works as you would expect. Mind you that some “friendly errors” may return
as “200 OK”, even though you’re getting something like “Site is down for
maintenance” or “server too busy to respond”.
In the event I am not provided with the complete file name, I want to check the
“Content-Type” so I can put the proper extension on the file when I save it.
Is there a better way to accomplish this than the code I put together below?
In a way, this question doesn’t make sense because you’re requesting the file
and should, therefore, know it’s name. But I think you are dealing with data
which is fed from a script, possibly fed from a database, so in these cases,
yes you would need to analyze the Content-Type: header to determine the data
type and then append the proper extension to the filename as you have done
below. This is the only option available to you and will work. Said another
way, if the server sends an incorrect Content-Type, a web browser would not be
able to display the image properly, therefore you should be able to depend on
it.
That said, as a last ditch effort, you might be able to introspect the file to
get its type. More times than not, the first few bytes of a file indicate it’s
type. I wouldn’t try coding this until you find that you need it.
The only minor comment I would make about your code is that there’s probably a
little faster way to get the data you need from the DOM:
Returns the Result Code:
<@ELEMENTVALUE object=URLContents xpath="/HTTP_RESPONSE/STATUS/CODE">
Returns the Content-Type:
<@ELEMENTVALUE object=URLContents
xpath="/HTTP_RESPONSE/HEADER[@NAME='Content-Type']">
Robert
Thanks
Steve Fogelson
Internet Commerce Solutions
<@assign request$Status '<@elementname object="request$URLContents"
xpath="/HTTP_RESPONSE/STATUS/*">'>
<@assign request$StatusValues '<@elementvalue object="request$URLContents"
xpointer="root().child(1).child(all)">'>
<@for start="1" stop="<@numrows array='request$Status'>">
<@if "<@var request$Status[<@currow>,1]> = 'CODE'">
<@assign request$CODE "<@var request$StatusValues[<@currow>,1]>">
<@break>
</@if>
</@for>
<@assign request$Names '<@elementattribute object="request$URLContents"
attribute="NAME" xpath="/HTTP_RESPONSE/*">'>
<@assign request$NameValues '<@elementvalue object="request$URLContents"
xpointer="root().child(all)">'>
<@for start="1" stop="<@numrows array='request$Names'>">
<@if "<@var request$Names[<@currow>,1]> = 'Content-Type'">
<@assign request$Content-TypeRow "<@currow>">
<@break>
</@if>
</@for>
<@if "<@var request$Content-TypeRow> > 0">
<@if "<@var request$NameValues[<@calc '<@var request$Content-TypeRow> +
1'>,1]> = 'image/gif'">
<@assign request$FileType "gif">
<@elseif "<@var request$NameValues[<@calc '<@var request$Content-TypeRow> +
1'>,1]> = 'image/jpeg'">
<@assign request$FileType "jpg">
<@elseif "<@var request$NameValues[<@calc '<@var request$Content-TypeRow> +
1'>,1]> = 'image/jpg'">
<@assign request$FileType "jpg">
<@elseif "<@var request$NameValues[<@calc '<@var request$Content-TypeRow> +
1'>,1]> = 'image/png'">
<@assign request$FileType "png">
</@if>
</@if>
_____
To unsubscribe from this list, please send an email to [email protected]
with "unsubscribe witango-talk" in the body.
----------------------------------------
To unsubscribe from this list, please send an email to [email protected]
with "unsubscribe witango-talk" in the body.