Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identifying bad text files

Status
Not open for further replies.

ManOnBar

Programmer
Jan 4, 2005
2
US
Using ASP I want to identify an uploaded Excel file that has had its extension changed to .csv, but the data in it is still binary, not text.

I have users uploading files that are suppose to be properly converted from Excel to text/csv format. Unfortunately some users take an Excel file and just manually change the extension from .xls to .csv, thus creating an unreadable text file when I go to retrieve it.

I tried to trap for ContentType, but I get "text/plain" as an answer, whether the file is a corrupt Excel file or legitamite text/csv file. Is there a way to to test the type of file in ASP beyond just using ContentType? Once I upload I try to do a bulk copy with the files using SQL Server and all I need is one of these files to be corrupt to crash everything with an ADO stream error.
 
The best approach is probably just to trap and gracefully handle the error.

You could open the file and get the ASC value of each byte and reject files with any character value > 127 but that just tells you the file can be read as ASCII text, not that it makes sense as an upload file.... for example it wouldn't guarantee the correct number and format of fields.
 
My problem is a Bad Excel file with a changed extension does not generate an ASP error during the upload, the file loads and is thought to be ContentType "text/plain" until the Bulk Copy procedure is invoked in SQL Server. At that point I cannot even trap in SQL Server, because the error is an ADO.stream that occurs within the Bulk Copy, thus the program crashes.

I need to identify that it is a bad file in classic ASP in some way so that I do not pass that file into the Bulk Copy procedure in SQL Server.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top