asp.net mvc - Uploading object images before saving the object to the database -


i have car form includes several properties , 2 buttons: first button save car properties form second 1 upload photos of car

the problem photos table has carid property , when car not saved carid null if user tries upload cars before saving car properties error comes if saved car portieres redirected main page , photos not uploaded

code upload images

public actionresult saveuploadedfile(int carid)     {         int id = convert.toint32(carid);         bool issavedsuccessfully = true;         string fname = "";         try         {             foreach (string filename in request.files)             {                 httppostedfilebase file = request.files[filename];                 //save file content goes here                 if (file != null)                 {                     fname = file.filename;                     if (file.contentlength > 0)                     {                          var originaldirectory = new directoryinfo($"{server.mappath(@"\")}images");                          var pathstring = path.combine(originaldirectory.tostring(), "carimages");                          var isexists = directory.exists(pathstring);                          if (!isexists)                             directory.createdirectory(pathstring);                          var path = $"{pathstring}\\{fname}";                         file.saveas(path);                         var image = new photoupload()                         {                             name = path.getfilenamewithoutextension(fname),                             extension = path.getextension(fname),                             carid = id                         };                         _context.photouploads.add(image);                         _context.savechanges();                     }                 }             }          }         catch (exception)         {             issavedsuccessfully = false;         }           if (issavedsuccessfully)         {             return json(new { message = fname });         }         else         {             return json(new { message = "error in saving file" });         }     } 

code save car

public actionresult save(cardto cardto)     {         var client = new httpclient { baseaddress = new  uri("http://localhost:54490") };          switch (cardto.id)         {             case 0:                 var id = user.identity.getuserid();                 cardto.carownerid = id;                 client.postasjsonasync<cardto>("api/cars", cardto)                     .continuewith((posttask) =>   posttask.result.ensuresuccessstatuscode());                 break;             default:                  client.putasjsonasync<cardto>("api/cars", cardto)                     .continuewith((posttask) =>  posttask.result.ensuresuccessstatuscode());                 break;          }          return new redirectresult(url.action("index", "manage", new         {             id = 1,             tab = "mycars"         }));      } 

could maybe upload images regardless under temp folder , return guid represent temp folder? when well, move photos folder them at, way, can link photos carid when user saves car.

or can enable upload photos button when there valid carid greater zero(assuming using identity(1,1) fro datatabse , or guid)


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -