Exemplo n.º 1
0
        //upload the images using usertalkid
        public int UploadModuleImages(UploadModuleImagesModel model)
        {
            int res = 0;

            try
            {
                using (TransactionScope trScope = new TransactionScope())
                {
                    using (SqlConnection con = new SqlConnection(CONNECTION_STRING))
                    {
                        con.Open();
                        SqlCommand command = new SqlCommand("Upload_Module_Image", con);
                        command.CommandType = CommandType.StoredProcedure;

                        command.Parameters.AddWithValue("@UserModuleImageId", model.UserModuleImageId);
                        command.Parameters.AddWithValue("@ImagePath", model.ImagePath);
                        command.Parameters.AddWithValue("@ModuleId", model.ModuleId);
                        command.Parameters.AddWithValue("@ModuleImageId", model.ModuleImageId);
                        command.Parameters.AddWithValue("@UserId", model.UserId);

                        res = Convert.ToInt32(command.ExecuteScalar());
                    }
                    trScope.Complete();
                }
            }
            catch
            {
                throw;
            }

            return(res);
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> UploadImage()
        {
            var httpRequest = HttpContext.Current.Request;
            int result      = 0;

            var model = new UploadModuleImagesModel();

            model.UserId            = Convert.ToInt32(httpRequest.Form[0]);
            model.ModuleId          = Convert.ToInt32(httpRequest.Form[1]);
            model.ModuleImageId     = Convert.ToInt32(httpRequest.Form[2]);
            model.UserModuleImageId = Convert.ToInt32(httpRequest.Form[3]);
            model.ModuleName        = httpRequest.Form[4];
            model.UserEmail         = httpRequest.Form[5];
            model.FacultyId         = Convert.ToInt32(httpRequest.Form[6]);

            foreach (string file in httpRequest.Files)
            {
                var postedFile = httpRequest.Files[file];
                if (postedFile != null && postedFile.ContentLength > 0)
                {
                    int MaxContentLength = 1024 * 1024 * 20; //Size = 20 MB

                    IList <string> AllowedFileExtensions = new List <string> {
                        ".jpg", ".jpeg", ".gif", ".png"
                    };
                    var ext       = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));
                    var extension = ext.ToLower();
                    if (!AllowedFileExtensions.Contains(extension))
                    {
                        throw new Exception("Please upload image of type .jpg, .jpeg, .gif, .png.");
                    }
                    else if (postedFile.ContentLength > MaxContentLength)
                    {
                        throw new Exception("Please upload a file upto 1 mb.");
                    }
                    else
                    {
                        var fileName = Guid.NewGuid().ToString() + extension;
                        var filePath = HttpContext.Current.Server.MapPath("~/Images/" + fileName);
                        postedFile.SaveAs(filePath);

                        model.ImagePath = "/Images/" + fileName;
                        result          = _businessLayer.UploadModuleImages(model, fileName);

                        return(Ok(result));
                    }
                }

                throw new Exception("Please upload a image.");
            }
            throw new Exception("Please upload a image.");
        }