Click here to Skip to main content
14,698,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all , i am new to mvc ... currently i am working on a file uploading which should be processed with proper validations . The most important is the file size validation . i came to know that it cannot be done using javascript ... So my code goes like this .
[HttpPost]
public ActionResult FileUpload(IEnumerable<HttpPostedFileBase> UploadedFile)
{
    try
    {
        List<string> uploaderror = new List<string>();
        int cnt = 1;

        foreach (var item in UploadedFile)
        {
            if (item != null)
            {
                if (item.ContentLength > 1048576)
                {
                    string test = "lbl_error" + cnt;
                    uploaderror.Add(test);
                    cnt++;
                }
            }
        }

        ViewData["UploadError"] = uploaderror;
        //var filename = Path.GetFileName(uploadFile.FileName);
        //var filesize = uploadFile.ContentLength;
    }
    catch (Exception ex)
    {

    }
    return View();
}


And my View is
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div id="Upload_Div">
        <%Html.BeginForm("FileUpload", "Home", FormMethod.Post, new { enctype="multipart/form-data" }); %>
        <ul>
            <li>
                <input type="file" id="File" name="UploadedFile"/>   <label name="lbl_error1" id="Label"></label></li>
                <br />
                <li><input type="file" id="File1" name="UploadedFile"/>  <label name="lbl_error2" id="Label1"></label></li>
                <br />
                <li><input type="file" id="File2" name="UploadedFile"/>  <label name="lbl_error3" id="Label2"></label></li>
                <br />
                <li><input type="file" id="File3" name="UploadedFile"/>  <label name="lbl_error4" id="Label3"></label></li>
                <br />
                <li><input type="submit" />
            </li>
            <li></li>
        </ul>
    </div>


My problem is how will i return to the same view and how will i use the viewdata["UploadError"] to show error message adjacent to repective upload control having file greater than 1 mb .


Thanks in advance :)
Posted
Comments
Prasad Khandekar 28-May-13 2:01am
   
Please have a look at this article (http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx)

1 solution

One Solution is when setting the upload error to the viewdata in the actionResult set fileUploadControl id also append with this string. for example

YourFile is more 1 mb:FileUpload id. to the viewdata. After returning the view by using javascript or jquery take the viewdata value like below

function YouJsFunction(){

var test = '@ViewData["UploadError"].ToString()';
//Split error and file upload id
}


After that create div by setting the error message using jquery or javascript and append the dive to the particular fileupload control we got.

Hope this helps
   
Comments
Abithdeveloper 28-May-13 2:41am
   
How can i go to the same view ????
Jameel VM 28-May-13 4:38am
   
return the same view in which your file upload control exist
Jameel VM 28-May-13 4:39am
   
for example return View().Which return the same view. I think that is you have already done

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




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