How to access/read from uploaded file

  1. #1

    How to access/read from uploaded file

    I've looked at this example and I don't understand how the FileUploadField works. The "filepath" is just the name of the file, which doesn't seem all that helpful to me. How are you supposed to be able to access the file or do anything with it?

    Eventually I would like to read from a spreadsheet, but could I get an example of reading an uploaded text file? Here is what I have to start:

    View:
    viewportItems.Add(Html.X().Window()
        .ID("UploadWindow")
        .Layout("AutoLayout")
            .Items(items =>
            {
                items.Add(Html.X().DisplayField()
                    .Text("Choose the file you would like to upload:"));
                items.Add(Html.X().FileUploadField()
                    .ID("FileUploadField"));
            })
            .Buttons(button =>
            {
                button.Add(Html.X().Button()
                    .ID("UploadButton")
                    .Text("Upload")
                    .DirectEvents(directEvent => {
                        directEvent.Click.Url = "~/Home/UploadFile";
                        directEvent.Click.CleanRequest = true;
                        directEvent.Click.Method = HttpMethod.POST;
                        directEvent.Click.ExtraParams.Add(new Parameter("fileLocation", "this.up('window').down('fileuploadfield').getValue()", ParameterMode.Raw));
                         }));
            }));
    Controller:
    public ActionResult UploadFile(string fileLocation)
    {
        //How to read from file?
    }
    Last edited by KBorkiewicz; Nov 20, 2012 at 9:52 PM.
  2. #2
    Hi KB,

    MVC is not my strong suit, but from what I can tell you need a form on the page, because the fileuploadfield submits data through form submission. You may also need to set the "IsUpload" property on your DirectEvent.Click object.

    Once the form has been submitted, the file is available in the Request.Files collection.
  3. #3
    Thank you. I tried adding a FormPanel and IsUpload=true, but I Request.Files gives me nothing.

    View:
     viewportItems.Add(Html.X().Window()
        .ID("UploadWindow")
        .Items(windowItems =>
        {
            windowItems.Add(Html.X().FormPanel()
                .ID("UploadSpreadsheetPanel")
                .Items(items => { 
                    items.Add(Html.X().DisplayField()
                        .Text("Choose the file you would like to upload:"));
                    items.Add(Html.X().FileUploadField()
                        .ID("FileUploadField"));
                    }));
                })
                .Buttons(button =>
                {
                    button.Add(Html.X().Button()
                        .ID("UploadButton")
                        .Text("Upload")
                        .DirectEvents(directEvent =>
                        {
                            directEvent.Click.IsUpload = true;
                            directEvent.Click.Url = "~/Home/UploadFile";
                            directEvent.Click.CleanRequest = true;
                            directEvent.Click.Method = HttpMethod.POST;
                        }));
        }));
    Controller:
    public ActionResult UploadFile()
    {
        Debug.WriteLine("file count is " + Request.Files.Count);
    }
    Prints "file count is 0".
    Last edited by KBorkiewicz; Nov 21, 2012 at 3:31 PM.
  4. #4
    Unfortunately, I can't see anything obviously wrong with your code, so I'm not sure how to advise you. I was able to get a simple example running locally, maybe it will help you.

    View:

    @{
        ViewBag.Title = "Ext.Net 2.0 MVC3 Example";
    }
    
    
    @(
        Html.X().ResourceManager()      
    )
    
    
    @(  
     Html.X().FormPanel()
            .ID("panelMain")
            .Width(400)
            .Height(250)
            .MarginSpec("15, 0, 0, 15")
            .BodyPadding(15)
            .Title("Upload Example")
            .Items(items =>
            {
                items.Add(Html.X().DisplayField()
                    .Text("Choose the file you would like to upload:"));
                items.Add(Html.X().FileUploadField()
                    .ID("FileUploadField")
                    .Width(300));
            })
            .Buttons(buttons =>
            {
                buttons.Add(Html.X().Button()
                    .ID("UploadButton")
                    .Text("Upload")
                    .DirectEvents(directEvents =>
                    {
                        directEvents.Click.IsUpload = true;
                        directEvents.Click.Url = "~/Home/UploadFile";
                        directEvents.Click.CleanRequest = true;
                        directEvents.Click.Method = HttpMethod.POST;                    
                    }));
            })
    )
    Controller:

    public class HomeController : System.Web.Mvc.Controller
    {        
        public ActionResult Index()
        {
            return View();
        }
    
    
        [HttpPost]
        public ActionResult UploadFile()
        {
            string filename = Request.Files[0].FileName;
            return new AjaxResult 
            { 
                Script = X.Msg.Alert("File Upload Alert", "You uploaded " + filename).ToScript(), 
                IsUpload = true 
            };
        }
    
    
    }

Similar Threads

  1. File uploader with the list of uploaded files
    By AlexMaslakov in forum 1.x Help
    Replies: 0
    Last Post: Aug 11, 2011, 11:56 AM
  2. [CLOSED] How to clear the uploaded file from fileuploadfield
    By Pablo_Azevedo in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Feb 21, 2011, 6:52 PM
  3. Replies: 0
    Last Post: Mar 31, 2010, 3:34 AM
  4. Controls Dont Dispaly Once Uploaded To Server
    By b.aytes in forum 1.x Help
    Replies: 8
    Last Post: Jul 29, 2009, 10:04 AM
  5. [CLOSED] AjaxRequestModule stopping normal file access
    By Steve in forum 1.x Legacy Premium Help
    Replies: 10
    Last Post: Apr 17, 2009, 6:11 AM

Tags for this Thread

Posting Permissions