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/
Related
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)
ENV: Asp.Net Vb / Visual Studio 2010 / .Net 4 IIS Express and IIS 6
I have a page called download.aspx which creates csv data from a database but cannot get browsers to download it with the right file name. My understanding was that Content-Disposition gives it a file name but the download is always the name of my page instead which is download.aspx.
I'm sure I have a misunderstanding of how this works and I've searched here for guidance but can't seem to get any solutions to work. I have the following:
Response.Clear()
Response.ContentType = "text/csv"
Response.AppendHeader("Content-Disposition", "attachment: filename=leadership.csv")
Response.Write("test,test,test")
Response.Flush()
Response.End()
I have tried "application/csv" and "application/x-download" with no differences. I have also tried it in a .ashx file with same file name issue. How can I get the file to come down as leadership.csv instead of download.aspx?
The Content-Dispostion header is wrong. The value
attachment: filename=leadership.csv
should be (with optional space)
attachment; filename=leadership.csv
Note that the :, which is used to separate headers from values, is changed to a ;, which is used to separate sub-values.
In addition, I would recommend using a library to generate the CSV, if not already the case ..
is it possible to create a binary excel file in asp classic?
the common method is using this
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment; filename=test.txt"
which is not a "true" excel file
In .net you could use the Microsoft.Office.Interop.Excel namespace - very easy to use, if you have access to .net i'd certainly go for that.
You say "in pure asp" - if this means without com objects etc - i dont think it's possible.
If however you wish to persevere with classic asp with the option of using a com object you can create full binary spreadsheets using the Interop com object - to do this you will need Excel installed on the webserver.
For example, to open a blank excel file on disk (i.e. a template), change a value and save the finished file:
'#### Ready Excel
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Application.Visible = True
'#### Open a blank Excel File
Set ExcelBook = ExcelApp.workbooks.Open("d:\wwwroot2\blankTemplate.xls")
'#### Select a sheet and change the value of a cell
ExcelBook.Worksheets("SheetName").Cells(1, 1).Value = "Lorem Ipsum"
'#### Switch between sheets
ExcelBook.Worksheets("AnotherSheetName").Select
'#### Save the finished file with a different name (to leave the template ready for easy re-use)
ExcelBook.SaveAs "d:\wwwroot2\FinishedFile.xls"
'#### Tidy up
ExcelBook.Close
ExcelApp.Application.Quit
Set ExcelApp = Nothing
Set ExcelBook = Nothing
Good thing about this method is the interop com object mimics VBA methods etc very closely, so if your ever unsure how to do something, record a macro in excel, view the raw vba, the code it produces, specifically the methods and parameters would be very similar in most cases to the code you would use for the com object.
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.
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)