コード例 #1
0
        public void UploadFiles(Page page)
        {
            string filename = string.Empty; string lastItem = string.Empty;

            string[] arr = null; string[] separators = { "\\" }; string ID = string.Empty;



            HttpFileCollection fileCollection = basepage.Request.Files;

            //NetworkCredential NCredentials = new NetworkCredential(ShareLocationUserName, ShareLocationPassword, ShareLocationDomain);

            //using (new NetworkConnection(ShareFolderPath, NCredentials))

            ////using (new ImpersonatedUser(ShareLocationUserName, ShareLocationDomain, ShareLocationPassword))

            //{

            //  DriveMapping.DelDrive("i:", 0, true);
            //DriveMapping.MapDrive(@"\\10.138.74.233\c$\MaqasaAttachmentDocumentUpload", "Y:", @"agilityindia\tmurugesan", "mugil#2302");

            /// using (new NetworkConnection(@"\\server2\write", writeCredentials))

            ImpersonateUser     iU            = new ImpersonateUser();
            string              sPwd          = "";
            SymmetricEncryption CgServices    = ServiceFactory.GetSymmetricEncryptionInstance();;

            sPwd = CgServices.DecryptData(ConfigurationSettings.AppSettings["ShareLocationPassword"].ToString());
            iU.Impersonate(ConfigurationSettings.AppSettings["ShareLocationDomain"].ToString(), ConfigurationSettings.AppSettings["ShareLocationUserName"], sPwd);

            for (int i = 0; i < fileCollection.Count; i++)
            {
                HttpPostedFile uploadfile = fileCollection[i];

                if (uploadfile.ContentLength > 0)
                {
                    if (uploadfile.ContentLength >= FileUploadSizeInKB)
                    {
                        //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", "alert('you cannot upload files more than 1MB');", true);
                        //return;
                        filename = uploadfile.FileName;
                        arr      = filename.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                        lastItem = arr[arr.Length - 1];
                        if (Language == "ara")
                        {
                            lblMessage.Text = "أقصى حجم للملفات التي يمكن تحميلها هو 500 كيلوبايت. حجم الملف أكبر من 500 كيلوبايت." + " " + lastItem;
                        }
                        else
                        {
                            lblMessage.Text = lblMessage.Text + lastItem + " " + "Size is too large. Maximum file size of 500 KB is allowed." + " <br/>";
                        }
                        // return;
                    }
                    else
                    {
                        ID = GetNewID("MaqasaDocuments");
                        string fileName     = Path.GetFileName(uploadfile.FileName);
                        string GetFileName  = Path.GetFileNameWithoutExtension(uploadfile.FileName);
                        string GetExtension = Path.GetExtension(uploadfile.FileName);

                        string Date             = DateTime.Now.ToString("yyyy-MM-dd") + "\\" + ParentId;
                        string FullfileName     = GetFileName + ID + GetExtension;
                        string FullPathFileName = Date + "\\" + FullfileName;

                        ShareFolderPath = ShareFolderPath + "\\" + Date;
                        if (!Directory.Exists(ShareFolderPath))
                        {
                            Directory.CreateDirectory(ShareFolderPath);
                        }
                        string strpath = ShareFolderPath + "\\" + FullfileName;
                        uploadfile.SaveAs(strpath);


                        //byte[] buffer = new byte[uploadfile.ContentLength];
                        //uploadfile.InputStream.Read(buffer, 0, uploadfile.ContentLength);



                        //string contentType = uploadfile.ContentType;
                        //Stream FileStream = uploadfile.InputStream;
                        //BinaryReader br = new BinaryReader(FileStream);
                        //byte[] myData = br.ReadBytes((Int32)FileStream.Length);
                        //// byte[] myData = new byte[uploadfile.ContentLength];

                        string query = "insert into " + ProfileName + " (" + TablePrimaryKey + ",DocumentName,ReferenceId,NewFileName,StateId,Description,DateCreated,ReferenceType,MaqasaDocumentType) values (@DocumentId,@DocumentName, @ReferenceId, @NewFileName,@StateId,@Description,@DateCreated,@ReferenceType,@MaqasaDocumentType)";

                        SqlParameter[] commandParameters = new SqlParameter[9];
                        commandParameters[0] = new SqlParameter();
                        commandParameters[0].ParameterName = TablePrimaryKey;
                        commandParameters[0].Value         = ID;

                        commandParameters[1] = new SqlParameter();
                        commandParameters[1].ParameterName = "@DocumentName";
                        commandParameters[1].Value         = fileName;

                        commandParameters[2] = new SqlParameter();
                        commandParameters[2].ParameterName = "@ReferenceId";
                        commandParameters[2].Value         = ReferenceId;

                        commandParameters[3] = new SqlParameter();
                        commandParameters[3].ParameterName = "@NewFileName";
                        commandParameters[3].Value         = FullPathFileName;

                        commandParameters[4] = new SqlParameter();
                        commandParameters[4].ParameterName = "@StateId";
                        commandParameters[4].Value         = ProfileName + "CreatedState";

                        commandParameters[5] = new SqlParameter();
                        commandParameters[5].ParameterName = "@Description";
                        commandParameters[5].Value         = ((TextBox)page.FindControl("txtDescription")).Text;

                        commandParameters[6] = new SqlParameter();
                        commandParameters[6].ParameterName = "@DateCreated";
                        commandParameters[6].Value         = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");

                        commandParameters[7] = new SqlParameter();
                        commandParameters[7].ParameterName = "@ReferenceType";
                        commandParameters[7].Value         = ReferenceType;

                        commandParameters[8] = new SqlParameter();
                        commandParameters[8].ParameterName = "@MaqasaDocumentType";
                        commandParameters[8].Value         = Convert.ToInt32(ddlDocumentTypes.SelectedValue);

                        SQLDataAccessHelper.Instance.ExecuteNonQuery(CommandType.Text, query, commandParameters);

                        //string constr = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;
                        //using (SqlConnection con = new SqlConnection(constr))
                        //{
                        //    //Commented by subramanyam for adding MaqasaDocumentType column
                        //    //string query = "insert into " + ProfileName + " (" + TablePrimaryKey + ",DocumentName,ReferenceId,NewFileName,StateId,Description,DateCreated,ReferenceType) values (@DocumentId,@DocumentName, @ReferenceId, @NewFileName,@StateId,@Description,@DateCreated,@ReferenceType)";
                        //string query = "insert into " + ProfileName + " (" + TablePrimaryKey + ",DocumentName,ReferenceId,NewFileName,StateId,Description,DateCreated,ReferenceType,MaqasaDocumentType) values (@DocumentId,@DocumentName, @ReferenceId, @NewFileName,@StateId,@Description,@DateCreated,@ReferenceType,@MaqasaDocumentType)";
                        //using (SqlCommand cmd = new SqlCommand(query))
                        //{
                        //    cmd.Connection = con;
                        //    cmd.Parameters.AddWithValue(TablePrimaryKey, ID);
                        //    cmd.Parameters.AddWithValue("@DocumentName", fileName);
                        //    cmd.Parameters.AddWithValue("@ReferenceId", ReferenceId);
                        //    cmd.Parameters.AddWithValue("@NewFileName", FullPathFileName);
                        //    cmd.Parameters.AddWithValue("@StateId", ProfileName + "CreatedState");
                        //    cmd.Parameters.AddWithValue("@Description", ((TextBox)page.FindControl("txtDescription")).Text);
                        //    cmd.Parameters.AddWithValue("@DateCreated", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
                        //    cmd.Parameters.AddWithValue("@ReferenceType", ReferenceType);
                        //    //Added by subramanyam
                        //    cmd.Parameters.AddWithValue("@MaqasaDocumentType", Convert.ToInt32(ddlDocumentTypes.SelectedValue));
                        //    con.Open();
                        //    cmd.ExecuteNonQuery();
                        //    con.Close();
                        //}
                        //}
                    }
                }
                else
                {
                    filename = uploadfile.FileName;
                    arr      = filename.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                    lastItem = arr[arr.Length - 1];
                    if (Language == "ara")
                    {
                        lblMessage.Text = lblMessage.Text + lastItem + " " + "الملف فارغ" + " <br/>";
                    }
                    else
                    {
                        lblMessage.Text = lblMessage.Text + lastItem + " " + "file is empty" + " <br/>";
                    }
                    //return;
                    //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", "alert('you did not specify a file to upload?');", true);
                    //return;
                }
            }

            iU.Undo();

            //}
            TextBox stxtDescription = ((TextBox)page.FindControl("txtDescription"));

            stxtDescription.Text           = "";
            ddlDocumentTypes.SelectedIndex = 0;
            BindGrid(GridView1);
        }
コード例 #2
0
        public void DownloadFiles(Page page, object sender)
        {
            try
            {
                byte[] bytes; string NewFileName = string.Empty;
                string fileName = string.Empty; string strpath = string.Empty;
                int    id = int.Parse((sender as LinkButton).CommandArgument);

                string         CommandText       = "select " + TablePrimaryKey + ",DocumentName, NewFileName from  " + ProfileName + "  where " + TablePrimaryKey + " =@Id";
                SqlParameter[] commandParameters = new SqlParameter[1];
                commandParameters[0] = new SqlParameter();
                commandParameters[0].ParameterName = "@Id";
                commandParameters[0].Value         = id;

                using (SqlDataReader sdr = SQLDataAccessHelper.Instance.ExecuteReader(CommandType.Text, CommandText, commandParameters))
                {
                    sdr.Read();
                    NewFileName = sdr["NewFileName"].ToString();
                    fileName    = sdr["DocumentName"].ToString();

                    ImpersonateUser     iU         = new ImpersonateUser();
                    string              sPwd       = "";
                    SymmetricEncryption CgServices = ServiceFactory.GetSymmetricEncryptionInstance();;
                    sPwd = CgServices.DecryptData(ConfigurationSettings.AppSettings["ShareLocationPassword"].ToString());
                    iU.Impersonate(ConfigurationSettings.AppSettings["ShareLocationDomain"].ToString(), ConfigurationSettings.AppSettings["ShareLocationUserName"], sPwd);

                    strpath = Path.Combine(ShareFolderPath, NewFileName);;
                    System.IO.FileStream fs = null;
                    fs    = System.IO.File.Open(strpath, System.IO.FileMode.Open);
                    bytes = new byte[fs.Length];
                    fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
                    fs.Close();
                    //}

                    iU.Undo();
                }

                //string constr = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;
                //using (SqlConnection con = new SqlConnection(constr))
                //{
                //    using (SqlCommand cmd = new SqlCommand())
                //    {
                //        cmd.CommandText = "select " + TablePrimaryKey + ",DocumentName, NewFileName from  " + ProfileName + "  where " + TablePrimaryKey + " =@Id";
                //        cmd.Parameters.AddWithValue("@Id", id);
                //        cmd.Connection = con;
                //        con.Open();
                //        using (SqlDataReader sdr = cmd.ExecuteReader())
                //        {
                //            sdr.Read();
                //            NewFileName = sdr["NewFileName"].ToString();
                //            fileName = sdr["DocumentName"].ToString();

                //            //NetworkCredential NCredentials = new NetworkCredential(ShareLocationUserName, ShareLocationPassword, ShareLocationDomain);

                //            //using (new NetworkConnection(ShareFolderPath, NCredentials))
                //            //{

                //            ImpersonateUser iU = new ImpersonateUser();
                //            string sPwd = "";
                //            SymmetricEncryption CgServices = ServiceFactory.GetSymmetricEncryptionInstance(); ;
                //            sPwd = CgServices.DecryptData(ConfigurationSettings.AppSettings["ShareLocationPassword"].ToString());
                //            iU.Impersonate(ConfigurationSettings.AppSettings["ShareLocationDomain"].ToString(), ConfigurationSettings.AppSettings["ShareLocationUserName"], sPwd);

                //            strpath = Path.Combine(ShareFolderPath, NewFileName); ;
                //            System.IO.FileStream fs = null;
                //            fs = System.IO.File.Open(strpath, System.IO.FileMode.Open);
                //            bytes = new byte[fs.Length];
                //            fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
                //            fs.Close();
                //            //}

                //            iU.Undo();
                //        }
                //    }
                //    con.Close();

                //}

                page.Response.Clear();
                page.Response.Buffer      = true;
                page.Response.ContentType = "application/octet-stream";
                page.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                page.Response.Charset = "";
                //Response.Cache.SetCacheability(HttpCacheability.NoCache);
                if (bytes.Length > 0)
                {
                    page.Response.OutputStream.Write(bytes, 0, (int)bytes.Length);
                }

                page.Response.Flush();
                //bytes=new Byte[test];
                page.Response.End();
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }