Ejemplo n.º 1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpFileCollection files = Request.Files;
        string             msg   = string.Empty;
        string             error = string.Empty;
        string             imgurl;
        string             input = "", DirRoot = "", filename = "";
        int stringindex = 0, cut = 0;

        if (files.Count > 0)
        {
            //files[0].SaveAs(Server.MapPath("/") + System.IO.Path.GetFileName(files[0].FileName));

            msg = "File size:" + files[0].ContentLength;

            input       = files[0].FileName;
            stringindex = input.LastIndexOf(@".");
            cut         = input.Length - stringindex;
            DirRoot     = input.Substring(stringindex + 1, cut - 1);

            // Allow only files less than (16 MB)=16777216 bytes to be uploaded.

            filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + @"." + DirRoot;

            Google.Apis.Auth.OAuth2.GoogleCredential credential = GCP_AUTH.AuthExplicit();
            imgurl = GCP_AUTH.upload_file_stream("", "upload/nursing_room_customer", filename, files[0].InputStream, credential);


            string res = imgurl;
            Response.Write(res);
            Response.End();
        }
    }
    protected void UploadDocument(object sender, EventArgs e)
    {
        string  input = "", DirRoot = "", filename = "";
        int     stringindex = 0, cut = 0;
        Boolean check = false;

        if (fuDocument.HasFile)
        {
            HttpPostedFile postedFile = fuDocument.PostedFile;

            DirRoot = System.IO.Path.GetExtension(postedFile.FileName).ToUpper().Replace(".", "");

            SqlDataSource2.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            SqlDataSource2.SelectCommand    = "select id,name from filename_extension";
            SqlDataSource2.DataBind();
            DataView ou1 = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
            for (int i = 0; i < ou1.Count; i++)
            {
                if (DirRoot.ToUpper() == ou1.Table.Rows[i]["name"].ToString().ToUpper())
                {
                    check = true;
                }
            }
            if (check)
            {
                int fileSize = fuDocument.PostedFile.ContentLength;

                // Allow only files less than (16 MB)=16777216 bytes to be uploaded.
                if (fileSize < 16777216)
                {
                    SqlDataSource sql_insert = new SqlDataSource();
                    sql_insert.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connStr"].ConnectionString;

                    filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + @"." + DirRoot;

                    //AmazonUpload aws = new AmazonUpload();
                    //string imgurl = aws.AmazonUpload_file("makelocal", "upload/test", filename, postedFile.InputStream);

                    Google.Apis.Auth.OAuth2.GoogleCredential credential = GCP_AUTH.AuthExplicit();
                    string imgurl = GCP_AUTH.upload_file_stream("makelocal", "upload/test", filename, postedFile.InputStream, credential);


                    //Label1.Text += fuDocument.FileName.ToString() + "  finish<br>";
                    //fuDocument.SaveAs(Server.MapPath("school_important_images") + "\\" + filename);
                    type0_Image.ImageUrl = imgurl;
                    Image4.Visible       = true;
                    Image5.Visible       = false;
                    //GridView1.DataBind();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(fuDocument, fuDocument.GetType(), "alert", "alert('File is out of memory 16MB!')", true);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(fuDocument, fuDocument.GetType(), "alert", "alert('filename extension is not in role!')", true);
            }
        }
    }
        public async System.Threading.Tasks.Task <ActionResult> Edit(Teams teams, HttpPostedFileBase image)
        {
            if (teams != null && ModelState.IsValid)
            {
                if (image != null)
                {
                    string filename = "";
                    filename = DateTime.Now.ToString("yyyyMMddTHHmmssfff");
                    Google.Apis.Auth.OAuth2.GoogleCredential credential = await Google.Apis.Auth.OAuth2.GoogleCredential.GetApplicationDefaultAsync();

                    _imageUploader = new ImageUploader(System.Web.Configuration.WebConfigurationManager.AppSettings["GoogleCloud:BucketName"]);
                    var imageUrl = await _imageUploader.UploadImage(image, filename, "Teams");

                    teams.imageURL = imageUrl;
                }


                teams.valid = 1;
                this.teamsRepository.Update(teams);
                return(RedirectToAction("Index"));
            }
            else
            {
                return(RedirectToAction("Index"));
            }


            /*try
             * {
             *  Mapper.Initialize(cfg => {
             *      cfg.CreateMap<TeamsViewModel, Teams>();
             *  });
             *  Teams teams = Mapper.Map<Teams>(mvm);
             *
             *
             *  if (string.IsNullOrEmpty(teams.imageURL) && image != null)
             *  {
             *      string filename = "";
             *      filename = DateTime.Now.ToString("yyyyMMddTHHmmssfff");
             *      Google.Apis.Auth.OAuth2.GoogleCredential credential = await Google.Apis.Auth.OAuth2.GoogleCredential.GetApplicationDefaultAsync();
             *      _imageUploader = new ImageUploader(System.Web.Configuration.WebConfigurationManager.AppSettings["GoogleCloud:BucketName"]);
             *      var imageUrl = await _imageUploader.UploadImage(image, filename, "Teams");
             *      teams.imageURL = imageUrl;
             *  }
             *
             *
             *  TeamsRepository mr = new TeamsRepository();
             *  teams.valid = 1;
             *  mr.UpdateTeams(teams);
             *
             *
             *  return RedirectToAction("Index");
             * }
             * catch (Exception e)
             * {
             *  return View(mvm);
             * }*/
        }
Ejemplo n.º 4
0
 public void AttachCredential(string CredentialFileLocation)
 {
     if (File.Exists(CredentialFileLocation))
     {
         m_CredentialFile = GoogleCred.FromFile(CredentialFileLocation);
     }
     else
     {
         System.Console.WriteLine("Credential file " + CredentialFileLocation + " not found.");
     }
 }
 /// <summary>
 /// Returns credentials with the scopes applied if required.
 /// </summary>
 /// <param name="credentials">Existing credentials, if any. This may be null,
 /// in which case the default application credentials will be used.</param>
 /// <returns>A task representing the asynchronous operation. The result of the task
 /// is the scoped credentials.</returns>
 public GoogleCredential GetCredentials(GoogleCredential credentials)
 {
     if (credentials != null)
     {
         return ApplyScopes(credentials);
     }
     try
     {
         // No need to apply scopes here - they're already applied.
         return _lazyScopedDefaultCredentials.Value.Result;
     }
     catch (AggregateException e)
     {
         // Unwrap the first exception, a bit like await would.
         // It's very unlikely that we'd ever see an AggregateException without an inner exceptions,
         // but let's handle it relatively gracefully.
         throw e.InnerExceptions.FirstOrDefault() ?? e;
     }
 }
Ejemplo n.º 6
0
    protected void UploadDocument(object sender, EventArgs e)
    {
        string  input = "", DirRoot = "", filename = "";
        int     stringindex = 0, cut = 0;
        Boolean check = false;

        Session["head_photo"] = null;
        if (fuDocument.HasFile)
        {
            HttpPostedFile postedFile = fuDocument.PostedFile;

            DirRoot = System.IO.Path.GetExtension(postedFile.FileName).ToUpper().Replace(".", "");

            Query = "select id,name from filename_extension";
            DataView ou1 = gc.select_cmd(Query);
            for (int i = 0; i < ou1.Count; i++)
            {
                if (DirRoot.ToUpper() == ou1.Table.Rows[i]["name"].ToString().ToUpper())
                {
                    check = true;
                }
            }
            if (check)
            {
                filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + @"." + DirRoot;

                Google.Apis.Auth.OAuth2.GoogleCredential credential = GCP_AUTH.AuthExplicit();
                string imgurl = GCP_AUTH.upload_file_stream("", "upload/test", filename, postedFile.InputStream, credential);
                //AmazonUpload aws = new AmazonUpload();
                //string imgurl = aws.AmazonUpload_file("", "upload/test", filename, postedFile.InputStream);

                //fuDocument.SaveAs(Server.MapPath("head_photo") + "\\" + filename);
                Image1.ImageUrl = imgurl;
                Image1.Attributes.Add("style", "display:block");

                Session["head_photo"] = "~/" + imgurl;
            }
            else
            {
                ScriptManager.RegisterStartupScript(fuDocument, fuDocument.GetType(), "alert", "alert('filename extension is not in role!')", true);
            }
        }
    }
 private GoogleCredential ApplyScopes(GoogleCredential original)
 {
     return original.IsCreateScopedRequired && _scopes.Count > 0
         ? original.CreateScoped(_scopes)
         : original;
 }
 /// <summary>
 /// Asynchronously returns credentials with the scopes applied if required.
 /// </summary>
 /// <param name="credentials">Existing credentials, if any. This may be null,
 /// in which case the default application credentials will be used.</param>
 /// <returns>A task representing the asynchronous operation. The result of the task
 /// is the scoped credentials.</returns>
 public Task<GoogleCredential> GetCredentialsAsync(GoogleCredential credentials) =>
     credentials == null
         ? _lazyScopedDefaultCredentials.Value
         : Task.FromResult(ApplyScopes(credentials));
Ejemplo n.º 9
0
    protected void UploadDocument(object sender, EventArgs e)
    {
        string  input = "", DirRoot = "", filename = "";
        int     stringindex = 0, cut = 0;
        Boolean check = false;


        if (fuDocument.HasFile)
        {
            foreach (HttpPostedFile postedFile in fuDocument.PostedFiles)
            {
                DirRoot = System.IO.Path.GetExtension(postedFile.FileName).ToUpper().Replace(".", "");


                Query = "select id,name from filename_extension";
                DataView ou1 = gc.select_cmd(Query);
                for (int i = 0; i < ou1.Count; i++)
                {
                    if (DirRoot.ToUpper() == ou1.Table.Rows[i]["name"].ToString().ToUpper())
                    {
                        check = true;
                    }
                }
                if (check)
                {
                    int fileSize = postedFile.ContentLength;

                    // Allow only files less than (16 MB)=16777216 bytes to be uploaded.
                    if (fileSize < 16777216)
                    {
                        filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + @"." + DirRoot;


                        //AmazonUpload aws = new AmazonUpload();
                        //string imgurl = aws.AmazonUpload_file("", "upload/test", filename, postedFile.InputStream);

                        Google.Apis.Auth.OAuth2.GoogleCredential credential = GCP_AUTH.AuthExplicit();
                        string imgurl = GCP_AUTH.upload_file_stream("", "upload/test", filename, postedFile.InputStream, credential);

                        //AmazonUpload aws = new AmazonUpload();
                        //string imgurl = aws.AmazonUpload_file("", "upload/test", filename, postedFile.InputStream);

                        Image im = new Image();
                        im.Width    = 100;
                        im.Height   = 100;
                        im.ImageUrl = imgurl;
                        this.Panel1.Controls.Add(im);

                        image_HiddenField.Value += ",~/" + imgurl;



                        //upload_files.Text += Server.MapPath("fileplace") + "\\" + filename + ",";
                        //upload_files0.Text += postedFile.FileName.ToString() + ",";
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(fuDocument, fuDocument.GetType(), "alert", "alert('File is out of memory 16MB!')", true);
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(fuDocument, fuDocument.GetType(), "alert", "alert('filename extension is not in role!')", true);
                }
            }
        }
    }
    protected void UploadDocument(object sender, EventArgs e)
    {
        string  input = "", DirRoot = "", filename = "";
        int     stringindex = 0, cut = 0;
        Boolean check = false;

        if (fuDocument.HasFile)
        {
            Panel1.Attributes.Add("style", "display:block");
            impath = new List <string>();
            Panel1.Controls.Clear();
            foreach (HttpPostedFile postedFile in fuDocument.PostedFiles)
            {
                DirRoot = System.IO.Path.GetExtension(postedFile.FileName).ToUpper().Replace(".", "");

                SqlDataSource2.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
                SqlDataSource2.SelectCommand    = "select id,name from filename_extension";
                SqlDataSource2.DataBind();
                DataView ou1 = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
                for (int i = 0; i < ou1.Count; i++)
                {
                    if (DirRoot.ToUpper() == ou1.Table.Rows[i]["name"].ToString().ToUpper())
                    {
                        check = true;
                    }
                }
                if (check)
                {
                    int fileSize = postedFile.ContentLength;

                    // Allow only files less than (16 MB)=16777216 bytes to be uploaded.
                    if (fileSize < 16777216)
                    {
                        SqlDataSource sql_insert = new SqlDataSource();
                        sql_insert.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connStr"].ConnectionString;

                        filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + @"." + DirRoot;

                        //AmazonUpload aws = new AmazonUpload();
                        //string imgurl = aws.AmazonUpload_file("", "upload/test", filename, postedFile.InputStream);

                        Google.Apis.Auth.OAuth2.GoogleCredential credential = GCP_AUTH.AuthExplicit();
                        string imgurl = GCP_AUTH.upload_file_stream("", "upload/test", filename, postedFile.InputStream, credential);

                        Image im = new Image();
                        im.Width    = 100;
                        im.Height   = 100;
                        im.ImageUrl = imgurl;
                        impath.Add(@"~/" + imgurl);
                        this.Panel1.Controls.Add(im);

                        //Image1.ImageUrl = Server.MapPath("fileplace") + "\\" + filename;


                        //upload_files.Text += Server.MapPath("fileplace") + "\\" + filename + ",";
                        //upload_files0.Text += postedFile.FileName.ToString() + ",";
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(fuDocument, fuDocument.GetType(), "alert", "alert('File is out of memory 16MB!')", true);
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(fuDocument, fuDocument.GetType(), "alert", "alert('filename extension is not in role!')", true);
                }
            }



            ViewState["myData"] = impath;
        }
    }