Yes I am using Classic ASP, not by choice I am supporting a Legacy application. Objective: I need to have a form page that submits to another .asp page that will upload the file and store it on the server in a certain directory such as "/uploads". I'm not real familiar with asp or asp.net so I am very new to this. I've created a test prototype:
Form page:
<!DOCTYPE html>
<head>
<title>Test upload</title>
</head>
<body>
<form action="process.asp" method="post" enctype="multipart/form-data">
<p>Filename: <input type="text" name="filename" size="50" /></p>
<p><input type="file" name="file" /><input type="submit" value="Upload file" /></p>
</form>
</body>
</html>
Processing page:
<%
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set tfolder = fs.GetSpecialFolder(2)
tname = fs.GetTempName
'Declare variables
Dim fileSize
Dim filename
Dim file
Dim fileType
Dim p
Dim newPath
'Assign variables
fileSize = Request.TotalBytes
fileName = Request.form("filename")
file = request.form("file")
fileType = fs.GetExtensionName(file)
fileOldPath = tfolder
newPath = Server.MapPath("/uploads/")
fs.MoveFile fileOrigPath, newPath
set fs = nothing
%>
The problem is that everytime I try to upload or run the script I get this error:
Microsoft VBScript runtime error '800a0035'
File not found
/tbird/fileUpload/process.asp, line 25
Obviously I'm not mapping correctly to the file and I think the major reason I am getting stuck is that in the first parameter of the MoveFile method I am not mapping to the file correctly. Can anyone tell me how I should be referencing the file or if I am doing it wrong?
Thanks in advance I would really appreciate the help I've searched all over and everything I find related to classic asp and uploading files are classes that you can purchase and I don't want to do that.
Have a look at a solution like Pure ASP Upload, it should help you. In classic ASP, you cannot directly access Request.Form when data is sent in multipart/form-data, so you have the choice of using a third party component like ASPUpload or a ASP class that does the work of parsing the request for you and exposing methods to save the file.
When moving files you must also specify the file names.
Change:
fs.MoveFile fileOrigPath, newPath
To:
fs.MoveFile fileOrigPath & fileName, newPath & fileName
Assuming "fileName" is the correct variable for the file name and not "file" variable above.
Related
#app.route('/registerdriver', methods=['POST'])
def register_driver():
fname = request.form['fname']
lname = request.form['lname']
email = request.form['email']
mobno = request.form['mobno']
password = request.form['password']
file = request.files['driving_license']
file.filename = mobno+"_"+fname
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
Above is the code I used for saving the file. However the following error pops out while trying to save the file
flask.debughelpers.DebugFilesKeyError
flask.debughelpers.DebugFilesKeyError: You tried to access the file
"driving_license" in the request.files dictionary but it does not
exist. The mimetype for the request is
"application/x-www-form-urlencoded" instead of "multipart/form-data"
which means that no file contents were transmitted. To fix this error
you should provide enctype="multipart/form-data" in your form.
The browser instead transmitted some file names.
Can someone help me with this
In your html form tag include
<form action="/path" method="post" enctype="multipart/form-data">
</form>
I'm working on an MVC4 C# project in VS2010.
I would like to allow the user to upload the contents of a .csv file to a database but there is a requirement to first echo the contents of the file to screen (as a final visual check) before submitting. What would be the best approach of submitting to the database as I am struggling to find a way of persisting the complex object in the view?
Here is the view where I am using a form to allow the user to upload the csv file:
#model IEnumerable<MyNamespace.Models.MyModel>
#{
ViewBag.Title = "Upload";
WebGrid grid = new WebGrid(Model, rowsPerPage: 5);
}
<h2>Upload</h2>
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" />
</form>
<h2>Grid</h2>
#grid.GetHtml(
//Displaying Grid here)
<p>
#Html.ActionLink("Submit", "Insert")
</p>
Here is the action in the controller that processes the csv file:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data"), fileName);
file.SaveAs(path);
//Stream reader will read test.csv file in current folder
StreamReader sr = new StreamReader(path);
//Csv reader reads the stream
CsvReader csvread = new CsvReader(sr);
List<MyModel> listMyModele = new List<MyModel>(); // creating list of model.
csvread.Configuration.RegisterClassMap<MyModelMap>(); // use mapping specified.
listMyModel = csvread.GetRecords<MyModel>().ToList();
sr.Close();
//return View();
return View(listMyModel);
}
Up until this point everything is simple, I can upload the csv to the controller, read using CsvHelper, produce a list of MyModel objects and display in the view within a grid. To reiterate my initial question, is it now possible to submit the complex object (the list of MyModel) from the view as I can't figure out a way of making it available to an action within the controller.
Thank you.
Yes it's possible, It's "easier" if you have a Model with the IEnumerable in it so you can use the naming convention like this:
Property[index].ItemProperty
for every Html input/select field.
If you want to keep the IEnumerable as Model I think the naming convention is something like this:
ItemProperty[index]
So translated in code:
#Html.TextBoxFor(t => t.Property, new { name = "Property[" + i + "]" })
where i comes from a for loop to render all items or something like that.
I have already done it but I can't find the code at the moment. KendoUI uses this scheme for its multirows edit in the grid component.
You can check their POST AJAX requests for the right naming convention.
EDIT 1:
Otherwise you can think about store the model somewhere temporarily and retrieve it every time and updating with user inputs. It's a little more expensive but probably easier to write. Something like an updated csv file or a temporary db table.
I was wondering if you read text in a certain div so when the html code says:
<html>
<head>
</head>
<body>
<div id="Main">SomeText</div>
<div id="Text">Welcome to my website</div>
</body>
</html>
i only want to see 'Welcome to my website' in the textbox 1.
is there anyone who knows how i can do that?
any help would be much appreciated.
Mark your div with runat="server":
<div id="TextDiv" runat="server">Welcome to my website</div>
then access the text in VB.NET code:
TextDiv.InnerHtml
I would recommend the HTML Agility pack hosted on codeplex at http://htmlagilitypack.codeplex.com/. With it you can connect to a HTML source, load the HTML into an reasonably friendly navigator and use XML type queries to traverse and manipulate the HTML.
I would use HtmlAgilityPack, then it's easy as:
Dim html = System.IO.File.ReadAllText("path")
Dim doc = New HtmlAgilityPack.HtmlDocument()
doc.LoadHtml(html)
Dim welcomeDiv = doc.GetElementbyId("Text")
Me.TextBox1.Text = welcomeDiv.InnerText
I'm trying to implement a file upload system and I don't really get what to do.
I'm looking for the easiest way to do it. After long researches, I've found those explanations.
forums.asp.net/t/1678157.aspx/2/10
So, here is the things I've done inside the view :
#Code
Dim fileName As String = ""
If (IsPost) Then
Dim uploadedFile = Request.Files(0)
fileName = Path.GetFileName(uploadedFile.FileName)
fileSavePath = Server.MapPath("~/Content/Uploads/" + fileName)
uploadedFile.SaveAs(fileSavePath)
End If
End Code
<form action="" method="post">
#FileUpload.GetHtml(
initialNumberOfFiles := 1,
allowMoreFilesToBeAdded := False,
includeFormTag := True,
uploadText := "Upload")
</form>
Problem : GetHtml is not a member of 'System.Web.UI.WebControls.FileUpload'
What can I do to fix this? Is it the good way to handle file uploads?
One problem can be, because you forgot the enctype="multipart/form-data" in your form post.
Then maybe your Upload plugin is not installed correctly.
I assume your are using WebMatrix, so this article can be of help
Scott Hanselman has a great article on implementing this here
The code is in C# but it should get you started
How do I upload a file, from a browser, using the Lua programming language?
I'm using the Orbit web framework
This sample comes straight from the orbit sample pages/test.op.
<form method="POST" enctype="multipart/form-data" action="test.op">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
$lua{[[
local f = web.input.file
upload = {}
if f then
local name = f.name
local bytes = f.contents
local dest = io.open(web.real_path .. "/" .. name, "wb")
if dest then
dest:write(bytes)
dest:close()
upload[1] = name
end
end
]]}
You can easily adapt this to a normal orbit post handler. You can also take a look at how I used it in my library project, but it's way more complicated than your typical usage I guess.