file uploading in asp.Net MVC3 - vb.net

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

Related

Replacement for deprecated TYPO3 ErrorpageMessage

After the upgrade of the extension tt_board to TYPO3 10 I stumble over deprecation #77164 - ErrorpageMessage and AbstractStandaloneMessage.
The official page only states that the new Fluid-based ErrorPageController class should be used instead.
However this controller class is just able to generate a HTML result string for me. But the second half of the job is missing. The HTML output must be shown somehow by TYPO3 in an error dialog raised by an exception.
Is there any simple solution for this?
Before:
use TYPO3\CMS\Core\Messaging\ErrorpageMessage;
...
$message = $languageObj->getLabel('error_no_permission');
$title = $languageObj->getLabel('error_access_denied');
$messagePage =
GeneralUtility::makeInstance(
ErrorpageMessage::class,
$message,
$title
);
$messagePage->output();
Now:
use TYPO3\CMS\Core\Controller\ErrorPageController;
...
$message = $languageObj->getLabel('error_no_permission');
$title = $languageObj->getLabel('error_access_denied');
$errorController =
GeneralUtility::makeInstance(
ErrorPageController::class
);
$content = GeneralUtility::makeInstance(ErrorPageController::class)->errorAction(
$title,
$message
);
Can I send this $content to a TYPO3 object which will do the error output for the extension?
In general you can show it an any possible way, but you can use flashMessages to show it in standardized kind.
Be aware that for the frontend you have to implement according CSS for the styling.
In fluid it's looking like this and is part of extensions created with extension_builder:
<f:flashMessages />
It might be possible that you have to implement it a bit different to loop about several messages. I never use it usually but I remember that implementation changed once and doubt that it's reflected by this kind of implementation.
Below you find the required details listed:
The implementation for flashMessages is described here:
https://docs.typo3.org/m/typo3/reference-coreapi/11.5/en-us/ApiOverview/FlashMessages/Index.html
The description of the flashMessage-ViewHelper you find here:
https://docs.typo3.org/other/typo3/view-helper-reference/11.5/en-us/typo3/fluid/latest/FlashMessages.html
The CSS you could probably take from bootstrap but reliable from the extension examples. In the CSS for the backend you could search it too.
Concerning your code:
I think you can leave away this, as it's not used:
$errorController =
GeneralUtility::makeInstance(
ErrorPageController::class
);
or you can use this code:
$errorController =
GeneralUtility::makeInstance(
ErrorPageController::class
);
$content = $errorController->errorAction(
$title,
$message
);

Upload image using lua

I have been trying simple image upload using lua and Openresty web framework. I found many solutions like
lua-resty-upload
lua-resty-post
Using lua-resty-post I got the form data now how do I upload it?
local resty_post = require 'resty.post'
local cjson = require 'cjson'
local post = resty_post:new()
local m = post:read()
ngx.say(cjson.encode(m))
As I'm new to lua I don't understand which one to use.
My requirement is very simple, I need a file attribute and want to upload on some place like in php move_uploaded_file. Is there any simple way to upload a file?
Found the solution. Using lua-resty-post.
upload.html
<form action="/uploadimage" method="post" enctype="multipart/form-data">
<input type="file" name="upload" accept="image/*">
<button>Upload</button>
</form>
nginx.conf
location /uploadimage {
content_by_lua_file image.lua;
}
image.lua
local resty_post = require "resty.post"
local post = resty_post:new({
path = "/my/path", -- path upload file will be saved
name = function(name, field) -- overide name with user defined function
return name.."_"..field
end
})
local m = post:read()

MVC4 C# - How to submit list of object that are being displayed to the user?

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.

write text inside div element use webbrowser vb.net

Need to write in element .
This is the HTML code
< div id="t" class="message-tools__textarea js-scroller" contenteditable="true" data-placeholder="Write a message (Enter to send)">XXXXXX< /div>
My question is how to write text like XXXXXX ?????
Add this:
<div id="t" runat="server" ...
And then in your codebehind:
t.InnerText = "XXXXXXX" ' or t.InnerHTML if you're adding HTML code).
So, if the text to be replaced is fixed and has only 1 instance within the HTML code, it should be rather simple:
Dim OrigCode, ModifiedCode as string
OrigCode = GetGoogleCodeFromURL ' get the code
ModifiedCode = OrigCode.Replace("XXXXXXX","ZZZZZZ")
Dim MyHTML as string = "<head>...</head><body><h1>Hello World!</h1><p> </p>" & _
ModifiedCode & "<p> </p><p>That's it.</p>"
But usually, things are a bit more complicated, so I'm not sure, if you expressed your wish precisely. Also, if the code is big and action is recursive, it might be better to break it into parts and handle only relevant part of it, due to performance issues.

Upload a file in Classic ASP without another class completely custom

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.