コード例 #1
0
        public string uploadEpreuve(UploadModel epreuve)
        {
            HttpPostedFileBase file     = epreuve.file;
            string             matiere  = epreuve.matiere;
            string             annee    = epreuve.annee;
            string             response = "1";

            if (file != null && file.ContentLength > 0)
            {
                try
                {
                    string path = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Epreuves/"), file.FileName);
                    file.SaveAs(path);
                    GestionConcourDbContext db = new GestionConcourDbContext();
                    Epreuves epr = new Epreuves();
                    epr.NomFichier = file.FileName;
                    epr.Matiere    = matiere;
                    epr.Annee      = annee;
                    db.Epreuves.Add(epr);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    response = ex.Message;
                }
            }
            else
            {
                response = "0";
            }
            return(response);
        }
コード例 #2
0
        public int Upload(UploadModel model)
        {
            int    msg            = 0;
            string uniqueFileName = null;

            if (model.fichier != null)
            {
                try
                {
                    String extension = Path.GetExtension(model.fichier.FileName);
                    //se positionner dans le dossier
                    string uploadFolder = Path.Combine(hostingEnvironment.WebRootPath, "epreuves");
                    //make a unique filename
                    Random r    = new Random();
                    int    rInt = r.Next(0, 10000);
                    uniqueFileName = rInt.ToString() + extension.ToLower();

                    /*//se positionner dans le dossier
                     * string uploadFolder = Path.Combine(hostingEnvironment.WebRootPath, "epreuves");
                     * //make a unique filename
                     * uniqueFileName = Guid.NewGuid().ToString() + "_" + model.fichier.FileName;
                     * //définir le chemin complet*/
                    string filePath = Path.Combine(uploadFolder, uniqueFileName);
                    //upload dans le fichier epreuve
                    FileStream stream = new FileStream(filePath, FileMode.Create);
                    model.fichier.CopyTo(stream);
                    stream.Close();
                    //Inserer le name dans la bd
                    Epreuves epreuve = new Epreuves
                    {
                        Matiere    = model.matiere,
                        Annee      = model.annee,
                        NomFichier = uniqueFileName
                    };

                    db.Add(epreuve);
                    db.SaveChanges();

                    msg = 1;
                } catch (Exception ex)
                {
                }
            }

            return(msg);
        }