Click here to Skip to main content
15,892,746 members

form submition with file upload

cs101000 asked:

Open original thread
ASP.NET
@using Microsoft.Web.Helpers;

@{
    if(IsPost)
    {
        //upload file
        var fileName = string.Empty;
        var fileSavePath = string.Empty;
        var uploadedFile = Request.Files[0];
        fileName = Path.GetFileName(uploadedFile.FileName);
        fileSavePath = Server.MapPath("~/App_Data/UploadedFiles/" + fileName);
        uploadedFile.SaveAs(fileSavePath);
    }
}
@{
    // Initialize page
    var FirstName = string.Empty;
    var LastName = string.Empty;
    int Gender = 1;
    var Cell = string.Empty;
    var Phone = string.Empty;
    var Email = string.Empty;;
    int LangSelect = 1; //Default value is English to Persian
    //var FilePath = string.Empty;    
    var Description = string.Empty;
    List<string> Errors = new List<string>();
    bool AuthenticatedUser = false;
    if(WebSecurity.IsAuthenticated)
    {
       AuthenticatedUser = true; 
    }
    // If this is a POST request, validate and process data
    if (IsPost)
    {
        if(!AuthenticatedUser)
        {
            FirstName = Request.Form["txtFirstName"];
            LastName = Request.Form["txtLastName"];
            Gender = Convert.ToInt32(Request.Form["GenderSelect"]);
            Cell = Request.Form["txtMobile"];
            Phone = Request.Form["txtPhone"];
            Email = Request.Form["txtEmail"];
        }
        LangSelect = Convert.ToInt32(Request.Form["LangSelect"]);
        //FilePath = Request.Form["FilePath"];
        Description = Request.Form["txtDescription"];
        //form validation
        if(!AuthenticatedUser)
        {
            if (Phone.Contains("-") || Phone.Contains("_"))
            {
                Errors.Add("error 1");
            }
        }
        // If all information is valid, enroll the order
        if(Errors.Count == 0)
        { 
            /*//upload file
            var fileName = string.Empty;
            var fileSavePath = string.Empty;
            var uploadedFile = Request.Files[0];
            fileName = Path.GetFileName(uploadedFile.FileName);
            fileSavePath = Server.MapPath("~/App_Data/UploadedFiles/" + fileName);
            uploadedFile.SaveAs(fileSavePath);*/
            //update db
            var db = Database.Open("Trans");
            int OrdersMaxId = 0;
            if(db.QueryValue("SELECT COUNT(*) FROM OrderDetails") != 0)
            {
                OrdersMaxId = db.QueryValue("SELECT MAX(Id) FROM OrderDetails");
            }
            /*var qMax = db.QueryValue("SELECT MAX(Id) FROM OrderDetails");
            if(!qMax.GetType().Equals(DBNull))
            {
                OrdersMaxId = qMax;
            }*/
            int OrderId = OrdersMaxId + 1;
            //insert order status information into OrderStatus
            db.Execute(
            "INSERT INTO OrderStatus (AuditNum, Price, Status, TransMsg) VALUES (@0, @1, @2, @3)", 
            999 + OrderId, null, 0, null);
            //logged in user
            if(AuthenticatedUser)
            {
                db.Execute(
                "INSERT INTO OrderDetails (Id, Lang, Description, Time, UserId, CustomerId, AuditNum) VALUES (@0, @1, @2, @3, @4, @5, @6)", 
                OrderId, LangSelect, Description, DateTime.Now, WebSecurity.CurrentUserId , null, 999 + OrderId );
            }
            //not logged in user
            else
            {
                int UnregMaxId = 0;
                if(db.QueryValue("SELECT COUNT(*) FROM Unregistered") != 0)
                {
                    UnregMaxId = db.QueryValue("SELECT MAX(Id) FROM Unregistered");
                }
                /*qMax = db.QueryValue("SELECT MAX(Id) FROM Unregistered");
                if(!qMax.GetType().Equals(DBNull))
                {
                    UnregMaxId = qMax;
                }*/
                int CustId = UnregMaxId + 1;
                //insert customer details into Unregistered
                db.Execute(
                "INSERT INTO Unregistered (Id, Gender, FirstName, LastName, Cell, Phone, Email) VALUES (@0, @1, @2, @3, @4, @5, @6)", 
                CustId, Gender, FirstName, LastName, Cell, Phone, Email);
                //insert order details into Order
                db.Execute(
                "INSERT INTO OrderDetails (Id, Lang, Description, Time, UserId, CustomerId, AuditNum) VALUES (@0, @1, @2, @3, @4, @5, @6)", 
                OrderId, LangSelect, Description, DateTime.Now, null, CustId, 999 + OrderId );
            }
            
            // Navigate back to the homepage and exit
            //Response.Redirect("~/");
        }
    }
}

@if (Errors.Count > 0)
{
    /*<div style="border-style: dotted; border-width: 2px; border-color: #f00">
        <p>@ErrorMessage</p> 
        <p>Please correct the errors and try again.</p>
    </div>*/
    <div id ="ErrorBorder">  <!--style="border-style: dotted; border-width: 2px; border-color: #f00; margin: 5px">-->
        <p>please correct these</p> <br/>
        <ul>
            @foreach(var err in Errors){
                <li style="margin: 4px">@err</li>
            }
        </ul>
    </div>
}

<!DOCTYPE html>

<html lang="en">
    <head>
        <!--<meta charset="utf-8" />-->
        <title>ثبت سفارش</title>
    </head>
    <body>
        <form method="post" name="OrderReg" title="فرم ثبت سفارش" action="" enctype="multipart/form-data">
            <fieldset>
                <legend>سفارش ترجمه</legend>
                @if(!AuthenticatedUser)
                {
                    <table>
                    <caption>فرم مشخصات</caption>
                    <tr>
                        <td>نام:</td>
                        <td><input type="text" name="txtFirstName" value="@FirstName"/></td>
                    </tr>
                    <tr>
                        <td>نام خانوادگی:</td>
                        <td><input type="text" name="txtLastName" value="@LastName"/></td>
                    </tr>
                    <tr>
                        <td rowspan="2">جنسيت:</td>
                        <td style="padding: 2px;">
                            <input type="radio" name="GenderSelect" value="1" style="padding: 0px"/>آقا<br/>
                            <!--<asp:radiobutton xmlns:asp="#unknown" name="GenderSelect" value="1" style="padding: 0px">آقا</asp:radiobutton>-->
                        </td>
                    </tr>
                    <tr>
                        <td style="padding: 2px">
                            <input type="radio" name="GenderSelect" value="0" style="padding: 0px"/>خانم
                            <!--<asp:radiobutton xmlns:asp="#unknown" name="GenderSelect" value="0" style="padding: 0px">خانم</asp:radiobutton>-->
                        </td>
                    </tr>
                    <tr>
                        <td>تلفن همراه:</td>
                        <td><input type="text" name="txtMobile" value="@Cell"/></td>
                    </tr>
                    <tr>
                        <td>تلفن ثابت:<br/><div style="font-size: smaller">(به همراه كد شهرستان)</div></td>
                        <td><input type="text" name="txtPhone" value="@Phone"/></td>
                    </tr>
                    <tr>
                        <td>آدرس ايميل:</td>
                        <td><input type="text" name="txtEmail" lang="en" value="@Email"/></td>
                    </tr>
                </table>
                }              
                <table>
                    <caption>سفارش</caption>
                    <tr>
                        <td rowspan="2">زبان مبداء و مقصد:</td>
                        <td>
                            <input type="radio" name="LangSelect" value="1"/>انگليسی به فارسی<br/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="radio" name="LangSelect" value="0"/>فارسی به انگليسی
                        </td>
                    </tr>
                    <tr>
                        <td>فايل متن مورد نظر:</td>
                        <!--Its deficult to customize-->
                        <td>
                            <!--<input type="file" name="FilePath"/>-->
                            @FileUpload.GetHtml(
                                initialNumberOfFiles:1,
                                allowMoreFilesToBeAdded:false,
                                includeFormTag:true,
                                uploadText:"Upload")
                            @if (IsPost) {
                                <span>File uploaded!</span> <br/>
                            }

                        </td>
                        
                    </tr>
                    <tr>
                        <td>توضيحات:</td>
                        <!--<td><input type="text" name="txtDescription" style="height: 200px;"/></td>-->
                        <td><textarea name="txtDescription" rows="5" style="width: 100%;"></textarea></td>
                    </tr>
                    <tr>
                        <td colspan="2" style="text-align: center"><input type="submit" value="ارسال"/></td>
                    </tr>
                </table>
            </fieldset>
        </form>
    </body>
</html>


All I want to do is to have a form with these fields including file upload. But Im confused because of having 2 submit buttons (1 for upload and 1 for form submition)I want to validate the fields and then save them and the uploaded file as an order in the db. How can I omit the fileupload helper's upload button and use my own submit?
Tags: HTML, ASP.NET, WebMatrix

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900