コード例 #1
0
        public ActionResult AddCategory(HttpPostedFileBase name, HttpPostedFileBase description)
        {
            if (ModelState.IsValid)
            {

            }
            else
            {

            }
            Category ct = new Category();
            ct.name = name.ToString();
            ct.description = description.ToString();
            db.Categories.Add(ct);
            db.SaveChanges();
            return View();
        }
コード例 #2
0
        /// <summary>
        /// Turns the posted file into a list of dictionaries
        /// </summary>
        /// <param name="file">The file w/ data (first row must be column names)</param>
        /// <param name="delimiter">Delimiter between data</param>
        /// <returns>List of dicts, or null</returns>
        private List<Dictionary<String, String>> GetDataFromFile(HttpPostedFileBase file, String delimiter)
        {
            if (file == null || file.ContentLength == 0)
            {
                System.Diagnostics.Debug.WriteLine("File null or length zero:" + file.ToString());
                return null;
            }

            List<Dictionary<String, String>> data = new List<Dictionary<string, string>>();

            try
            {
                // Read the file stream
                using (StreamReader reader = new StreamReader(file.InputStream))
                {
                    // Keys and a dictionary for easier lookup
                    String[] keys = reader.ReadLine().Split('|');

                    // Loop through lines and grab data
                    String line = "";
                    while ((line = reader.ReadLine()) != null)
                    {
                        // Put this line into the data map
                        Dictionary<String, String> dataRow = new Dictionary<string, string>();
                        String[] values = line.Split(new string[] { delimiter }, StringSplitOptions.None);
                        for (int i = 0; i < keys.Length; i++)
                            dataRow.Add(keys[i], i < values.Length ? values[i] : "");
                        data.Add(dataRow);
                    }
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message + "\n" + e.StackTrace);
                return null;
            }

            return data;
        }
コード例 #3
0
        public static Imagem ImagemUpload(System.Web.HttpPostedFileBase arquivo, string pasta)
        {
            Imagem ImgRet = new Imagem();

            ImgRet.Ok       = false;
            ImgRet.Mensagem = string.Empty;
            String NewDir = string.Empty;

            NewDir = Domain.Util.config.PathUpImg + pasta + "\\";

            if (!string.IsNullOrEmpty(arquivo.ToString()) && arquivo.ContentLength > 0)
            {
                // BLOQUEIA A TRANSFERÊNCIA DE ARQUIVOS MAIOR QUE 1MB
                if (arquivo.ContentLength < (1048576 / 2))
                {
                    String   fileExtension     = System.IO.Path.GetExtension(arquivo.FileName);
                    String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };

                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        if (fileExtension == allowedExtensions[i])
                        {
                            ImgRet.Ok = true;
                        }
                    }

                    if (ImgRet.Ok)
                    {
                        try
                        {
                            //Verificando se um diretório existe, retorna uma variavel BOOL (true/false):
                            //String Path = Directory.GetCurrentDirectory();
                            Boolean bExistDirectory = Directory.Exists(NewDir);
                            if (!bExistDirectory)
                            {
                                Directory.CreateDirectory(NewDir);
                            }
                            //salva
                            arquivo.SaveAs(NewDir + arquivo.FileName);
                        }
                        catch (Exception ex)
                        {
                            // MENSAGEM INFORMATIVA PARA O USUÁRIO
                            ImgRet.Ok       = false;
                            ImgRet.Mensagem = ex.Message;
                        }
                    }
                    else
                    {
                        // MENSAGEM INFORMATIVA PARA O USUÁRIO
                        ImgRet.Ok       = false;
                        ImgRet.Mensagem = "ArquivoInvalido";
                    }
                }
                else
                {
                    // MENSAGEM INFORMATIVA PARA O USUÁRIO
                    ImgRet.Ok       = false;
                    ImgRet.Mensagem = "TamanhoMaximo500kb";
                }
            }

            return(ImgRet);
        }