Skip to main content

Posts

asp.net - How to rename file when upload in FileUpload

Rename uploaded file FileUpload is an asp.net web server control that allows users to upload a file to a web server from their local computer. FileUpload server control displays a text box and a browse button on the web browser to select a file to upload from the client's local machine. FileUpload control does not automatically save the uploaded file to the web server. FileUpload SaveAs() method allows us to save the contents of a client-uploaded file to a web server hard disk in a specified path. Using the FileUpload control's HasFile property we can verify that the FileUpload control contains a file. FileUpload control's FileName property gets the client-uploaded file name. FileUpload control's PostedFile property allows us to get the additional properties on the uploaded file such as FileName, ContentLength, ContentType, etc. We can change the client-uploaded default file name by using FileUpload control's SaveAs m...

asp.net - How to validate a FileUpload control

FileUpload control validation FileUpload is an ASP.NET web server control that allows us to upload a file to the web server from the client browser. FileUpload server control renders a TextBox and a browse Button in the web browser that enable users to select a file from the client computer and upload it to the web server machine. Users can specify the file to upload by entering the full path of the local computer file in the TextBox of FileUpload control or using the browse button to select a file from the local computer file system. RequiredFieldValidator control allows us to check whether the FileUpload control has a file selected. So users must input a file path in FileUpload control before submitting the form. We can make the FileUpload control a required field in web form by attaching a RequiredFieldValidator control with it. RequiredFiledValidator is an asp.net validation web server control that makes an input control to a require...

asp.net - How to filter file extension in FileUpload control

Upload file with specific extension FileUpload is an asp.net web server control that allows users to upload a file to a web server from their local computer file system. FileUpload control does not save files automatically to the server. FileUpload control's SaveAs() method allows us to save client-uploaded files to the web server file system. SaveAs() method needs to pass a parameter named 'filename'. By default, FileUpload control uploads any valid type file from the client machine. Sometimes .net developers need to restrict file type to upload. Such as in an application we can allow users to upload only JPEJ image files. Next, paragraphs describe how can we filter extensions when uploading a file to a web server. First, we specify the web server folder to save the uploaded file. Second, we check the FileUpload control contains a file by FileUpload HasFile property. This method validates FileUpload control in server-side code...

How to check whether a file exists or not in asp.net

Check whether a file exists The File class’s Exists() method allows us to determine whether the specified file exists. File.Exists(path) method is under the System.IO namespace. So we need to include the System.IO namespace on our page before using this method. The Fileclass’s Exists() method needs to pass a parameter named 'path'. The path parameter type is a String and it represents the file to check. The path value contains the full path of a file including both folder location and file name with extension. The method returns a Boolean value. If it returns 'True' then the file exists in the web server otherwise, the file does not exist. If the path parameter describes a directory then the method returns False. Because this method can only determine file existence not for use to check whether a directory exists. This method also returns False, if any error occurs while trying to determine file existence. This method should not ...

How to copy file and overwrite in asp.net c#

File copy and overwrite .NET Framework File class’s Copy(String, String, Boolean) overloaded method allow us to copy an existing file to a new file. Overwriting a file of the same name is allowed. File class’s Copy() method exists in the System.IO namespace. The Copy method requires passing three parameters named sourceFileName, destFileName, and overwrite. The sourceFileName parameter value data type is a String which represents the file to copy. The destFileName parameter value data type is also a String which represents the name of the destination file. This parameter cannot be a directory. The overwrite parameter value data type is a Boolean. this parameter value true indicates the destination file can be overwritten. If we don't want to allow destination file overwrite then we need to set this parameter value to false. The Copy() method throws an UnauthorizedAccessException exception if the caller does not have the required perm...

How to remove an item from session in asp.net

Remove an item from session Asp.net session state allows us to store and retrieve values for a user as the user navigates asp.net pages in a website. HTTP is a stateless protocol. Session state makes an asp.net application stateful that allows us to save, update, remove, and read user data. We can delete an item from the session state collection by using HttpSessionState Remove() method as Session Remove(). This Session Remove(name) method requires passing a parameter. This parameter name is 'name' and its data type is String. The Remove() method exists under System.Web.SessionState namespace. The 'name' parameter value is the name of the item to delete from the session state collection. If the session state collection does not contain an element with the specified name (that passes by parameter), the collection remains unchanged. No exception is thrown. The following asp.net c# example code demonstrates to us how can we re...

How to clear the current session data in asp.net

Session Clear() Method The ASP.NET application uses the session state to store and retrieve the user's data. The application store data when the user navigates ASP.NET pages on a website. We can add an item (variable) to the session state collection using Session Add() method and we can remove a variable from the session state collection by using Session Remove() method. ASP.NET session state collection store each item as a key-value pair. In this pair, the key is the item name and the value is the specified item's value. HttpSessionState class’s Clear() method allows us to remove all keys and values from the current session state collection. We can use this method in an ASP.NET application as the Session Clear() method. This method has no parameter and it has no return value. The Session Clear() method exists under System.Web.SessionState namespace. The following ASP.NET C# example code demonstrates to us how can we clear the curr...