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.
Related
I was trying to upload some images in my website using input type=file in my asp.net mvc project.while storing the image I used the following code and it is saving the image successfully.
var path2 = Path.Combine(Server.MapPath("~/Images"), filename.jpg);
artwork.SaveAs(path2);
After that I need to save the whole link ( http://example.com/Images/filename.jpg) into my table column.
for that i tried this
tablename.imagelink = path2 ;
then in table column I am getting like
D:\xxxxx\yyyy\example\Images\filename.jpg
then i tried to save the whole link directly
tablename.imagelink = Path.Combine(Server.MapPath("http://example.com/Images/"), filename.jpg);
at that time I am getting an error: "is not a valid virtual path error"
How to solve this. ?
Server.MapPath requires the virtual path of the web server, and you get the error because you're passing the full path like this:
Server.MapPath("http://example.com/Images/")
You need to keep using this:
Server.MapPath("~/Images")
Assume I have a file that contains :
<%
Response.write("<my_tag>value</my_tag>")
%>
If I get it as an ordinary XML file, I get an error telling me that the XML have not the right format because it begin by "<%". How can I read this XML dynamically generated ?
Edit:
In fact, it was an illusion. The Server.Execute method just print the other file. What can I do ? How could I put the result of an ASP page in a string that I could read by loadXML method ? Or how could I just process the file before loading it ?
Give it a file extension that ASP will know to process, or tell ASP to process the file extension you're using. The server has to know to process the file and give you the dynamic result. It doesn't treat every file served as a classic ASP file, you have to tell it what to treat the file as if you're using a non-standard extension for ASP. You can do this by mapping the classic ASP handler to the file type you're trying to HTTP GET.
Use an .asp extension and set the Content Type:
<%
Response.CharSet = "utf-8"
Response.Buffer = True
Response.ContentType="text/xml"
Response.Write "<?xml version=""1.0"" encoding=""utf-8""?>"
Response.Write "<my_tag>value</my_tag>"
Response.Flush
%>
You might take a look here: http://www.w3.org/TR/REC-xml/
I am currently in a situation where I have to make some additions to an application written in classic ASP using server-side JScript on IIS.
The additions that I need to make involve adding a series of includes to the server-side code to extend the application's capabilities. However, the inc files may not exist on the server in all cases, so I need the application to fall back to the existing behavior (ignore the includes) if the files do not exist, rather than generating an error.
I know that this can't be accomplished using if statements in the JScript code because of the way that SSI works, and have not come across any ways of dynamically including the code on the server side, where the files may not exist.
Does anyone know of a way to accomplish this in classic ASP? Any help would be much appreciated.
Here's a script to dynamically include asp files:
<%
' **** Dynamic ASP include v.2
function fixInclude(content)
out=""
if instr(content,"#include ")>0 then
response.write "Error: include directive not permitted!"
response.end
end if
content=replace(content,"<"&"%=","<"&"%response.write ")
pos1=instr(content,"<%")
pos2=instr(content,"%"& ">")
if pos1>0 then
before= mid(content,1,pos1-1)
before=replace(before,"""","""""")
before=replace(before,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
before=vbcrlf & "response.write """ & before & """" &vbcrlf
middle= mid(content,pos1+2,(pos2-pos1-2))
after=mid(content,pos2+2,len(content))
out=before & middle & fixInclude(after)
else
content=replace(content,"""","""""")
content=replace(content,vbcrlf,""""&vbcrlf&"response.write vbcrlf&""")
out=vbcrlf & "response.write """ & content &""""
end if
fixInclude=out
end function
Function getMappedFileAsString(byVal strFilename)
Dim fso,td
Set fso = Server.CreateObject("Scripting.FilesystemObject")
Set ts = fso.OpenTextFile(Server.MapPath(strFilename), 1)
getMappedFileAsString = ts.ReadAll
ts.close
Set ts = nothing
Set fso = Nothing
End Function
execute (fixInclude(getMappedFileAsString("included.asp")))
%>
The last line (the one starting with "execute") is equivalent to an "include" directive, with the difference that it can be included inside an "if" statement (dynamic include).
Bye
If you are really brave, you can read the contents of the file and then Eval() it.
But you will have not real indication of line numbers if anything goes wrong in the included code.
As a potentially better alternative: Can you not create some sanity check code in global.asa to create the include files as blanks if they do not exist?
Put simply, no. Why would the files not exist? Can you not at least have empty files present?
What you could do is something like this:
Use Scripting.FileSystemObject to detect the presence of the files
Use Server.Exeecute to "include" the files, or at least execute the code.
The only problem is that the files cannot share normal program scope variables.
The solution to this turned out to be to use thomask's suggestion to include the files and to set a session variable with a reference to "me" as per http://www.aspmessageboard.com/showthread.php?t=229532 to allow me to have access to the regular program scope variables.
(I've registered because of this, but can't seem to associate my registered account with my unregistered account)
In my project I want to rename the file before it is updating. For example a file in my system like Mycontact.xls. I want to rename it as sasi.xls (it is an excel file). How can I write the code in ASP.NET?
Actually I am using a fileupload control to get the file in and rename the file and upload the renamed file in a folder which is in Solution Explorer.
You can do it with the File.Move method eg:
string oldFileName = "MyOldFile.txt";
string newFileName = "MyNewFile.txt";
File.Move(oldFileName, newFileName);
C# does not provide a file rename function, unfortunately. Anyhow, the idea is to do this:
File.Copy(oldFileName, NewFileName);
File.Delete(oldFileName);
You can also use - File.Move.
Be aware that when that code executes, the owner of the file will turn into the identity you have set on your Application Pool on which the website is running.
That account might not have enough permissions to 'create new' or 'delete' files.
I would advise you to place all read/writable files in a seperate location so you can control the security settings seperately on that part. This will also split off the 'only readable files/executables' (like the aspx and such) from the 'read/writable' files.
I have a page something.aspx, with associated codebehind something.aspx.cs. In that codebehind, I want to know the filesystem location of something.aspx. Is there any convenient way to get it?
Update: I got several excellent answers, which unfortunately didn't work because of something else crazy I'm doing. I'm encoding some additional information on the URL I pass in, so it looks like this:
http://server/path/something.aspx/info1/info2/info3.xml
The server deals with this OK (and I'm not using querystring parameters to work around some other code that I didn't write). But when I call Server.MapPath(Request.Url.ToString()) I get an error that the full URL with the 'info' segments isn't a valid virtual path.
// File path
string absoluteSystemPath = Server.MapPath("~/relative/path.aspx");
// Directory path
string dir = System.IO.Path.GetDirectoryName(absoluteSystemPath);
// Or simply
string dir2 = Server.MapPath("~/relative");
Request.PhysicalPath
Server.MapPath is among the most used way to do it.
string physicalPath = Server.MapPath(Request.Url);
Server.MapPath( Request.AppRelativeCurrentExecutionFilePath )