使用文件上传形成子目录 [英] form submition with file upload

查看:78
本文介绍了使用文件上传形成子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@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?



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?

推荐答案

You can have multiple submit buttons for the same form, but nesting forms is not allowed in html. If you look at the generated code, you will see, that this helper will generate a form. Be aware, that originally it was built for asp.net web forms, not for web pages nor mvc.

Set includeFormTag:false in @FileUpload.GetHtml (see: http://evonet.com.au/webmatrix-uploading-images-with-the-fileupload-helper/[^]), so your outer form will post the file input field also.
You can have multiple submit buttons for the same form, but nesting forms is not allowed in html. If you look at the generated code, you will see, that this helper will generate a form. Be aware, that originally it was built for asp.net web forms, not for web pages nor mvc.
Set includeFormTag:false in @FileUpload.GetHtml (see: http://evonet.com.au/webmatrix-uploading-images-with-the-fileupload-helper/[^]), so your outer form will post the file input field also.


这篇关于使用文件上传形成子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆