Sending Pictures to a .NET Server using C#

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!

Answer №1

While I may not have all the details on the Question at hand, if you're looking to store an image in C#, this resource might be of assistance: Troubleshooting saving an uploaded image

Additionally, make sure to take into consideration any permissions that may be enforced by the Windows operating system.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Show the flex items arranged horizontally

This template is generated dynamically by Django: <div class="center row"> <h3><span>The Core</span></h3> {% for member in core %} <a class="core_img " href="#"> <div class="img__overlay"> ...

Achieving perfect alignment for images within a Bootstrap 4 carousel - both vertically and horizontally

Utilizing Bootstrap 4 carousel, I am attempting to center an image both vertically and horizontally. Below is my code along with the styling that I have applied: <div id="myCarousel" class="carousel slide" data-ride="carousel"> <ul class="car ...

The Extender Provider was unsuccessful in delivering an Extender back

After moving from .NET 2.0 to .NET 3.5, a colleague of mine is encountering an issue when trying to run the ASP .NET web project in Visual Studio 2008 debugger. While he can build the project without any problems, he is unable to debug it due to a run-time ...

What causes an error in a list<> in C#?

Hello there! I am a newbie when it comes to classes and objects, and I've encountered an error that is preventing my code from navigating to the desired page. Below is the snippet of my code. Your assistance would be greatly appreciated! WebService1.a ...

What is the best way to set the z-index for elements within CSS containers that are utilized for container queries?

I am thrilled about the introduction of container queries in CSS. It's great to see this feature now available across all browsers. However, I have encountered an issue when working with multiple consecutive containers and placing an absolutely positi ...

CSS z-index property not functioning properly for Flowplayer on Firefox browser

I am attempting to create a layered div using z-index, with one of the layers containing a flowplayer. Once the layer is properly created, I plan to update the div's z-index value based on the logic of the program. The following code functions correct ...

Is it possible to access the CSS property from an external CSS file even when inline styles are used?

Is there a way to retrieve the css property from an external source using JavaScript or jQuery even if inline styles are applied to the same element? Here's an example: <div id="my" style='left:100px'>some content</div> <styl ...

Show a pop-up form when a user focuses on it using React

Hello, I have been looking for a way to create an overlay with a form that appears when a specific input field is clicked. I am trying to achieve this using React. Can someone please advise me on how to accomplish this? Here is my code import React, { Co ...

Decrease the size of the MutiView page, excluding the page number

Within the multiview on my website, each view section is comprised of various user controls. In particular, one of the user controls within a page view that is not currently selected is causing performance issues. This user control contains a combobox tha ...

What is the best way to align content to the right of an image within a card using bootstrap?

I'm struggling to position the content on the right side of an image in a card below the image using bootstrap and CSS. Despite searching online and in the bootstrap documentation, I haven't been able to find a solution to my issue. When the scre ...

Position the center div within a table cell and left-align the text

I am working with a wrapper/list structure using ul and li elements. The ul element is set to display: table-row;, while the li element is set to display: table-cell;. I am facing an issue where I need to center the li element within the list, but align th ...

JS and MUI are combining forces to create a dynamic background change on the page when selecting items from a dropdown menu

I encountered an unusual issue on my website that appears to be related to the UI library I am using. Whenever I click on a select element, the background undergoes a slight change in width, causing the background image to flicker. Initially, I assumed thi ...

Displaying an array value instead of a new value list in a React component

Situation - Initial number input in text field - 1 List of Items - 1 6 11 Upon removing 1 from the text field, the list becomes - New List Items - NaN NaN NaN Now, if you input 4 in the field. The updated List Items are - NaN NaN 4 9 14 Expected ...

Adjusting the width of images in Bootstrap is proving to be a challenge

Check out this website. There is an advertisement image below the navigation bar. I am having trouble adjusting its width. I want the image to expand and fill the empty white space. I've attempted using relative and absolute positioning, but the width ...

What is the best way to position two divs side by side in a way that maximizes the available space?

I am facing an issue with two divs in my HTML code. The right one is set to 80px wide while the other should fill up the remaining space. I have tried various CSS configurations but it seems that the right box always ends up below the left box instead of b ...

Creating a dynamic content space: A step-by-step guide

Project Overview I'm currently working on a cinema demo website, where I have implemented four movie poster panels displaying the poster image, title, and screening times. These panels are meant to be clickable. My goal is to have an interactive fea ...

Adjust the dimensions of the thead, tbody, and td elements on the fly

I've implemented this property in a .css file on the table shown below and there are 9 records. .fixed tbody td, thead th { width: 5.2%; float: left; } In my scenario, when there are 4 columns, the width should be 23.2%; for 5 columns ...

Is there a simple method within C#'s standard library to verify if a number is a multiple of another number?

Is there an efficient way to determine if there is an integer a, given integers b and n, such that a^n=b? I am looking for a solution better than the inefficient code provided below. private static bool FindPowerBase(int b, int n) { for(int a = 1; ...

How can I perform updates on my C# application that is available on the Microsoft Store?

As a Mac developer venturing into the world of Windows app development, I am currently working on an app that will soon be available in the Windows Store. My main question is: do apps downloaded from the Windows Store need to independently check for new v ...

What is the best way to emphasize the tree path of a selected node in a TreeView control?

I am looking to emphasize the TreePath all the way up to the root node of the selected Node. protected void Trv_SelectedNodeChanged(object sender, EventArgs e) { HighlightPath(Trv.SelectedNode); } private void HighlightPath(T ...