Upload file in folder using asp.net c#



You can upload files or images, follow this code.

 
Add this HTML in yours .aspx page 
 
 
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="form-element-list mg-t-30">
<div class="cmp-tb-hd">
<h2>Upload File</h2>
</div>
<div class="row">
<div class="col-lg-12 col-md-4 col-sm-4 col-xs-12">
<div class="form-group">
<label>Example Attachment</label>
<asp:FileUpload runat="server" ID="FuFile"></asp:FileUpload>
</div>
</div>
</div>
<div class="form-example-int mg-t-15 text-center">
<asp:LinkButton runat="server" ID="LnkBlogSubmit" class="btn btn-success notika-btn-success waves-effect" ValidationGroup="Blog" Onclick="LnkConfirmationYes_Click"  Text="Submit"></asp:LinkButton>
<asp:LinkButton runat="server" ID="LnkBlogBack" class="btn btn-warning notika-btn-success waves-effect" PostBackUrl="dashboard" Text="Back"></asp:LinkButton>
</div>
</div>
</div>
</div>
 
 
After added HTML , add .aspx.cs
 
 
This function is return status of upload file in TRUE or FALSE  and also return 
FileUpload fileUpload  - Pass the file upload control.
string folder - Pass the folder name, which you want to store the file 
ref string uploadedFileName - get the file name 
 
protected void LnkConfirmationYes_Click(object sender, EventArgs e)
{
string uploadedFileName = string.Empty;
bool isExampleUploaded = UploadDocument(FuFile, "../folderName", ref uploadedFileName);
}
public bool UploadDocument(FileUpload fileUpload, string folder, ref string uploadedFileName)
{
string baseUrl = HttpContext.Current.Request.Url.Host;
bool isUploaded = false;
string saveLocation = string.Empty;
string fileName = string.Empty;
if ((fileUpload.PostedFile != null) && (fileUpload.PostedFile.ContentLength > 0))
{
fileName = System.IO.Path.GetFileName(fileUpload.PostedFile.FileName);
fileName = (DateTime.UtcNow.Ticks + "_" + fileName);
saveLocation = HttpContext.Current.Server.MapPath(folder) + fileName;
try
{
fileUpload.PostedFile.SaveAs(saveLocation);
isUploaded = true;
}
catch (Exception)
{
isUploaded = false;
}
}
else
{
isUploaded = false;
}
uploadedFileName= fileName;
return isUploaded;
}
 
 
This function you can use anywhere as your requirement.
 

0 Comment's

Comment Form

Submit Comment