As a newcomer to .NET, I have been conducting numerous tests and have encountered a problem that I hope someone can help me with. My goal is to enable users to upload a photo and save it to a specific path. While researching on MSDN, I came across information on using the <asp:~>
tag with variables inside it. However, whenever I try to implement this code, I receive an error stating "asp.net attributes are only allowed in asp.net files." This project aims to provide similar functionality to a node project I developed for a friend.
You can find a basic example of what I'm trying to achieve here at Codepen.
While Codepen lacks the capability to 'save' due to security measures, I am searching for a C# alternative. The code snippet closest to my desired outcome includes:
In the Controller:
@{
WebImage photo = null;
var newFileName = "";
var imagePath = "";
if(IsPost){
photo = WebImage.GetImageFromRequest();
if(photo != null){
newFileName = Guid.NewGuid().ToString() + "_" +
Path.GetFileName(photo.FileName);
imagePath = @"images\" + newFileName;
photo.Save(@"~\" + imagePath);
}
}
}
And in the view:
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend> Upload Image </legend>
<label for="Image">Image</label>
<input type="file" name="Image" />
<br/>
<input type="submit" value="Upload" />
</fieldset>
</form>
<h1>Uploaded Image</h1>
@if(imagePath != ""){
<div class="result">
<img src="@imagePath" alt="image" />
</div>
}
I encountered issues rendering the "@if" section in the cshtml, so I removed it from the view. It seems like there might be some differences between cshtml and ASP.NET.
If anyone could offer guidance or assistance, it would be greatly appreciated.
The functionality I aim to achieve is simple - User uploads file by hitting 'Upload', selects files, and I save the file with a unique name based on provided information.
This problem may also stem from how I positioned the controller. I assumed it should go into the controller responsible for this page's function.
Currently, when I select and hit 'Upload', the page simply refreshes without any visible changes. If the file is being saved, I am unaware of its location.
Thank you!