Popular Posts

Thursday, September 24, 2015

Thread in c#

Below is the code to use thread in c# and WPF for responsive UI.

void Main()
{
 ThreadStart ts = new ThreadStart(Test);
            Thread th = new Thread(ts);
            th.IsBackground = true;
            th.Priority = ThreadPriority.Lowest;
            th.Start();
}
void Test()
{
//TODO lengthy work
}

Wednesday, September 23, 2015

Freeze GridView Header While Scrolling

First create a CSS class as following.

.HeaderFreez { position:relative ; top:expression(this.offsetParent.scrollTop); z-index: 10; } and Set Gridview’s HeaderStyle CssClass as below. CssClass="HeaderFreez"

Thursday, September 17, 2015

Upload file using FTP

Due to security reason we can't upload file to server using HTTP, so best solution is to use FTP for the same.

Below is the complete solution for upload file to server using FTP. keep in mind user those are using should be write permission on the upload folder.

HTML--

 <form id="form1" runat="server">
        <div style="height:100px; border:medium solid #808080; margin:10px; padding:10px;">
            <asp:FileUpload ID="fileUpload" runat="server" />
            <asp:Button ID="Button1" Text="Upload" Width="100px" Height="30px" runat="server" OnClick="cpUploads" />
            <hr />
            <asp:Label ID="lblMessage" runat="server" />
        </div>
    </form>


CS Code--
protected void cpUploads(object sender, EventArgs e)
        {
            string cpPlusServerIP = "ftp.YOURSITE.com/";
            string serverUserName = "USERNAME";
            string serverUserPassword = "PASSWORD";
            byte[] fileBytes = null;
            FtpWebRequest cpFTPRequest;
            using (StreamReader fileStream = new StreamReader(fileUpload.PostedFile.InputStream))
            {
                fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
                fileStream.Close();
            }
            cpFTPRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + cpPlusServerIP + "/" + fileUpload.FileName));
            cpFTPRequest.Credentials = new NetworkCredential(serverUserName, serverUserPassword);
            cpFTPRequest.KeepAlive = false;
            cpFTPRequest.UseBinary = true;
            cpFTPRequest.ContentLength = fileUpload.FileName.Length;
            cpFTPRequest.Method = WebRequestMethods.Ftp.UploadFile;
            try
            {
                using (Stream cpRequestStream = cpFTPRequest.GetRequestStream())
                {
                    cpRequestStream.Write(fileBytes, 0, fileBytes.Length);
                    cpRequestStream.Close();
                }
                lblMessage.Text = "File Uploaded";
            }
            catch (Exception exception)
            {
                //TODO
                //Redirect to error handler page with user friendly massege
            }
        }