check file existence in FreeASPUpload - asp-classic

I want to check file if i uploaded it before. If file exist, i want to upload with different name. How to do this?
on error resume next
Set Upload = New FreeASPUpload
<!-- Upload.OverwriteFiles = False-->
Upload.Save(UploadPathValue)
if err <> 0 then
Response.Write(err.description)
end if

You will need to use the filesystemobject to check if the file exists before saving it.
Here is an explanation of how to do this: ASP fileexists

Related

Can't connect to mapped drive in GetFolder for FileSystemObject ASP Classic

I am trying to connect to R:\database\attendanceData\ a mapped drive to be used in the following code
sDir = "R:\database\attendanceData\"
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set obj_FolderBase = fso.GetFolder(sDir)
if obj_FolderBase.Files.Count = 0 then '=== Check if Attendance record data is in
response.write " No Attendance Data Found!"
response.End
end if
response.write "IT is working"
response.End
The file structure
But I get the following error
However if I move my files to C: drive, everything is working.
But it needs to be in a shared drive. Do I need to set some permission at R: or am I missing something, please help.

Determine DateLastModified for a web base JPG Image via ASP or ASPNet

Using VBS in an ASP page or ASPNet page, I would like to determine the date when a remote web-based JPG file was created (or modified, etc). For example a webcam shot which was FTP-ed to the website. Thus, with the URL of the JPG, how can I get to the properties of that JPG.
In the code below I tried to retrieve the image file and save it into my site web, but I get an error on the "objADOStream.SaveToFile (strSave,2)" line which says that
"System.Runtime.InteropServices.COMException: Write to file failed.".
I know I have read/write permissions to that folder as I regularly create/delete .txt files there.
Can you comment on why I am not able to save this file?
Assuming I do get it saved will the original file properties be retained?
Or, maybe even better, is there an easier way to get this photo file information?
Here is the code that I cobbled together to retrieve and save the file
dim strURL, strSave, objXMLHTTP, objADOStream, objFSO
strURL = "http://www.somewhere.com/Photo.jpg"
strSave = Server.MapPath("/xxx/photos/") & "photo.jpg"
objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
objXMLHTTP.open ("GET", strURL, false)
objXMLHTTP.send()
if err.number=0 then
if objXMLHTTP.readystate = 4 then
If objXMLHTTP.Status = 200 Then
objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1
objADOStream.Write (objXMLHTTP.ResponseBody)
objADOStream.Position = 0
objFSO = Createobject("Scripting.FileSystemObject")
If objFSO.Fileexists(strSave) Then objFSO.DeleteFile (strSave)
objFSO = Nothing
objADOStream.SaveToFile (strSave,2)
objADOStream.Close
objADOStream = Nothing
End If
end if
end if
objXMLHTTP = Nothing
Thanks...RDK
If all you need is to know the last modification date of the file, instead of sending a GET request, send a HEAD request. In your code change GET with HEAD and, after send get
objXMLHTTP.getResponseHeader("Last-Modified")
It is possible that the server don't send this information, so you have no way to know this data.

FileUpload control in asp/VB.net cannot access the file because it is being used by another process

Error message:
The process cannot access the file 'C:\SampleProjectName\mytestcsv.csv' because it is being used by another process.
I am trying to read numerous files (CSV, XML, HTML) in asp/VB.net using the fileupload (file upload) control.
I'm saving the file using Server.MapPath so I can process the file in another procedure. It's very odd, but sometimes I can browse and upload the same file over and over with no issues, but sometimes it immediately fails.
I've found that I can ultimately kill the WebDev.WebServer40.exe it releases whatever lock is present. This is annoying, but fine for my debugging... but unacceptable for endusers.
My fileupload code:
If fuImport.HasFile Then
If (System.IO.File.Exists(Server.MapPath("myhtml.html"))) Then
System.IO.File.Delete(Server.MapPath("myhtml.html"))
End If
Dim dtFromHTML As New Data.DataTable
Dim dtFromSQL As New Data.DataTable
Try
fuImport.SaveAs(Server.MapPath("mytestcsv.csv"))
'Process data here
ProcessCSVData(Server.MapPath("mytestcsv.csv"))
Catch ex As Exception
Response.Write("error: " & ex.Message)
Finally
fuImport.PostedFile.InputStream.Flush()
fuImport.PostedFile.InputStream.Close()
fuImport.FileContent.Dispose()
End Try
'Other things happen here
Else
Response.Write("no file...")
End If
Any ideas would be appreciated.
Use FileShare.Read to read a file even if it is opened exclusively by an another process.
You may not be releasing the file in your code for access. You should use the keyword "using".
Read this post:
The process cannot access the file because it is being used by another process - using static class
and this:
http://msdn.microsoft.com/en-us/library/htd05whh.aspx
I am not a VB.NET coder but try something along the lines of:
Using {fuImport.SaveAs(Server.MapPath("mytestcsv.csv")) }
ProcessCSVData(Server.MapPath("mytestcsv.csv"))
End Using
The next procedure that uses file...
Try closing input stream before accessing the file:
UPDATED To use temp filename
Dim sTempName = Path.GetRandomFileName
fuImport.SaveAs(Server.MapPath(sTempName))
'close before accessing saved file
fuImport.PostedFile.InputStream.Close()
'Process data here
ProcessCSVData(Server.MapPath(sTempName)

.txt embed in .asp file

I'm new to ASP and having a little trouble.
A CMS is pushing out data into .txt files. I do not have the option to change what the CMS outputs, so they have to be .txt files.
a text file named textfile.txt looks like this:
widetxt=<P align='left'><B>Hello world!</B></P>&done=1
I need to display the "widetxt" variable on an .asp page.
The directory structure is like this:
ASP file is at the root of a folder, textfile.txt is located in a folder named "txt" off of the root folder.
index.asp
[txt]
|----textfile.txt
I tried the below code in the asp file, but I get a 500 error: "500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed."
<%
Set fs = CreateObject("Scripting.FileSystemObject")
Set wfile = fs.OpenTextFile(Server.MapPath("txt/textfile.txt"),1,true))
filecontent = wfile.ReadAll
wfile.close
Set wfile=nothing
Set fs=nothing
response.write(filecontent)
%>
I know for a fact both files are on the server and are where they are supposed to be.
If i remove the above code, and just put:
<%
response.write("Hello World!")
%>
the asp file works. So something in the OpenTextFile code is wrong, but i do not have the experience to know what it is.
Any help would be appreciated.
Set wfile = fs.OpenTextFile(Server.MapPath("txt/textfile.txt"),1,true))
You have one too many ) at the end of this statement. Every ( should have a matching ).
Set wfile = fs.OpenTextFile(Server.MapPath("txt/AttachmentFix.txt"),1,true)
Also, I don't see the remainder of your code, but after your response.write(filecontent) make sure to set filecontent as Nothing.
Set filecontent = Nothing
Also, when you're developing in Classic ASP #jsobo is right - you should have Friendly Error messages disabled as you can see what errors the script is throwing back.

how to check whether a file exists before creating it

I am creating an xml file. I need to check first if the file exists or not. If the file does not exist, create it and add the data cmg from a .cs file.
If the file exists, don't create the file just add the data cmg from a .cs file.
My code looks like this:
string filename="c:\\employee.xml";
XmlTextWriter tw=new XmlTextWriter(filename,null);//null represents
the Encoding Type//
tw.Formatting=Formatting.Indented; //for xml tags to be indented//
tw.WriteStartDocument(); //Indicates the starting of document (Required)//
tw.WriteStartElement("Employees");
tw.WriteStartElement("Employee","Genius");
tw.WriteStartElement("EmpID","1");
tw.WriteAttributeString("Name","krishnan");
tw.WriteElementString("Designation","Software Developer");
tw.WriteElementString("FullName","krishnan Lakshmipuram Narayanan");
tw.WriteEndElement();
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();
so next time we add data to file we need to check if the file exits and add data to xml file
and as we have made empID as a primary key, if user tries to make duplicate entry we need to avoid
Is this possible to do?
if (!File.Exists(filename))
{
// create your file
}
or
if (File.Exists(filename))
{
File.Delete(filename);
}
// then create your file
File class is in System.IO namespace (add using System.IO; to your file)
You can't append records to an XML file, you have to read the file and then rewrite it.
So, just check if the file exists, and read the records from it. Then write the file including all previous records and the new record.
Have a look at the File.Exists method here
Testing for existance of a file before attempting to create it inherently is subject to a "things change after check" race condition. Who can guarantee you that your application isn't preempted and put to sleep for a moment after you checked, someone else creates/deletes that file, your app gets to run again and does exactly the opposite of what you intended ?
Windows (as well as all UN*X variants) supports file open/create modes that allow to perform that create-if-nonexistant/open-if-existant operation as a single call.
As far as .NET goes, this means for your task (create an XML file) you'd first create a System.IO.FileStream with the appropriate modes, see http://msdn.microsoft.com/en-us/library/system.io.filemode.aspx and then pass that stream to the XmlWriter constructor. That's safer than simply performing an "exists" check and hoping for the best.

Resources