How to upload file and generate unique URL to display file in ASP.NET MVC -


i have working code upload csv-file view on same page. want view each file on unique url. so, this:

  1. user choses csv-file upload
  2. submits
  3. gets redirected unique url (e.g. /index/1234, 1234 unique id - each file get's uploaded has unique id let's unique id should "filename" or something)
  4. on unique url, content of file gets displayed

i want list on index every file uploaded get's displayed, , user can choose file view.

view

            @using read_csv_mvc.models             @model ienumerable<customermodel>             @{                 layout = null;             }              <!doctype html>              <html>             <head>                 <meta name="viewport" content="width=device-width" />                 <title>index</title>                 <style type="text/css">                     body {                         font-family: arial;                         font-size: 10pt;                     }                      table {                         border: 1px solid #ccc;                         border-collapse: collapse;                         background-color: #fff;                     }                          table th {                             background-color: #b8dbfd;                             color: #333;                             font-weight: bold;                         }                          table th, table td {                             padding: 5px;                             border: 1px solid #ccc;                         }                          table, table table td {                             border: 0px solid #ccc;                         }                 </style>             </head>             <body>                 @using (html.beginform("index", "home", formmethod.post, new { enctype = "multipart/form-data" }))                 {                     <input type="file" name="postedfile" />                     <input type="submit" value="import" />                 }                 @if (model.count() > 0)                 {                     <hr />                     <table cellpadding="0" cellspacing="0">                         <tr>                             <th>customer id</th>                             <th>name</th>                             <th>country</th>                         </tr>                         @foreach (customermodel customer in model)                         {                             <tr>                                 <td>@customer.customerid</td>                                 <td>@customer.name</td>                                 <td>@customer.country</td>                             </tr>                         }                     </table>                 }             </body>             </html> 

controller

        using system;         using system.collections.generic;         using system.linq;         using system.web;         using system.web.mvc;         using system.io;         using read_csv_mvc.models;          namespace read_csv_mvc.controllers         {             public class homecontroller : controller             {                 // get: home                 public actionresult index()                 {                     return view(new list<customermodel>());                 }                  [httppost]                 public actionresult index(httppostedfilebase postedfile)                 {                     list<customermodel> customers = new list<customermodel>();                     string filepath = string.empty;                     if (postedfile != null)                     {                         string path = server.mappath("~/uploads/");                         if (!directory.exists(path))                         {                             directory.createdirectory(path);                         }                          filepath = path + path.getfilename(postedfile.filename);                         string extension = path.getextension(postedfile.filename);                         postedfile.saveas(filepath);                          //read contents of csv file.                         string csvdata = system.io.file.readalltext(filepath);                          //execute loop on rows.                         foreach (string row in csvdata.split('\n'))                         {                             if (!string.isnullorempty(row))                             {                                 customers.add(new customermodel                                 {                                     customerid = convert.toint32(row.split(',')[0]),                                     name = row.split(',')[1],                                     country = row.split(',')[2]                                 });                             }                         }                     }                      return view(customers);                 }             }         } 

model

        using system;         using system.collections.generic;         using system.linq;         using system.web;          namespace read_csv_mvc.models         {             public class customermodel             {                 ///<summary>                 /// gets or sets customerid.                 ///</summary>                 public int customerid { get; set; }                  ///<summary>                 /// gets or sets name.                 ///</summary>                 public string name { get; set; }                  ///<summary>                 /// gets or sets country.                 ///</summary>                 public string country { get; set; }             }         } 

any appreciated, thanks!

in controller need change

    public actionresult index(string id)     {          string path = server.mappath("~/uploads/");         var files = directory.getfiles(path);         list<customermodel> customers = new list<customermodel>();                   if (!string.isnullorwhitespace(id) && files.firstordefault(x => x.contains(id)) !=null)            {             string filepath = files.firstordefault(x => x.contains(id));             string csvdata = system.io.file.readalltext(filepath);             //execute loop on rows.             foreach (string row in csvdata.split('\n'))             {                 if (!string.isnullorempty(row))                 {                     customers.add(new customermodel                     {                         customerid = convert.toint32(row.split(',')[0]),                         name = row.split(',')[1],                         country = row.split(',')[2]                     });                 }             }         }         viewbag.files = files.select(x => x.remove(x.lastindexof("."), x.length - x.lastindexof(".")).replace(path, "")).toarray();         return view(customers);     }     [httppost]     public actionresult index(httppostedfilebase postedfile)     {         list<customermodel> customers = new list<customermodel>();             string path = server.mappath("~/uploads/");         string filepath = string.empty;         if (postedfile != null)         {             if (!directory.exists(path))             {                 directory.createdirectory(path);             }              filepath = path.combine(path, postedfile.filename);             postedfile.saveas(filepath);             string csvdata = system.io.file.readalltext(filepath);             //execute loop on rows.             foreach (string row in csvdata.split('\n'))             {                 if (!string.isnullorempty(row))                 {                     customers.add(new customermodel                     {                         customerid = convert.toint32(row.split(',')[0]),                         name = row.split(',')[1],                         country = row.split(',')[2]                     });                 }             }         }         var files = directory.getfiles(path);         viewbag.files = files.select(x => x.remove(x.lastindexof("."), x.length - x.lastindexof(".")).replace(path, "")).toarray();         return view(customers);     } 

and in view add div inside body tag.

 <div>     @if (viewbag.files !=null)     {         foreach (var item in (string[])viewbag.files)         {             @html.actionlink(item, "index", new { id = item })                <br />         }     } </div> 

it works


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -