Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
please tell me how to upload File without refresh the page in asp.net using c#. after complete uploading, display every message of store data from file to sqlserver.
Posted
Comments
VICK 24-Oct-13 0:57am    
If you want to avoid postback .. you can simply place your aspFileUploader in update Panel and use AsyncPostbackTrigger to avoid full page post back..or else use ajaxfile uploader ocntrol as mentioned in solution 1.
Abhilask kumar 24-Oct-13 3:12am    
I have do this step but problem occur. i will send you code
<asp:UpdatePanel runat="server" ID="excelfile">
<contenttemplate>
<td><asp:FileUpload ID="file_excel" runat="server" accept=".xlsx,.xls" />
<br />

Max. File Size 200kb


</td>

<Triggers>
<asp:PostBackTrigger ControlID="btnsave" />
</Triggers>
VICK 24-Oct-13 4:14am    
see my answer posted.

Hello Abhilask kumar,

Visit "AjaxFileUpload Demonstration" from www.asp.net

Link to article: http://goo.gl/WL7MWw[^]

cheers,
jafc
 
Share this answer
 
Kindly try to use the following



XML
<asp:UpdatePanel>
<ContentTemplate>


//Your file Uploader

        </ContentTemplate>
         <Triggers>

             <asp:AsyncPostBackTrigger ControlID="btngtProvider" EventName="Click"/>
       </Triggers>
        </asp:UpdatePanel>




Hope it will help. :)
 
Share this answer
 
Comments
Abhilask kumar 24-Oct-13 4:23am    
After Click on button then file not selected.
Abhilask kumar 24-Oct-13 4:23am    
use masterpage .
VICK 24-Oct-13 6:12am    
Have you tried this???

or same solution without Triggers???

Try it without trigger and than check.
We can use one Generic handeler and some javascript code to work out this
FileUpload.aspx:-
---------------
<script>
$(documnet).ready(function(){('#mul_FileUpload').change(function () {
onDrop(this);
});});

function onDrop(e) {
var files=null;
ReadFile(files);
}
function ReadFile(files){
var file=files[0];
var filename = getFileName(file.name);
var reader = new FileReader();
reader.onerror = function (e) {
alert('Error code: ' + e.target.error);
};// Create a closure to capture the file information.
reader.onload = (function (aFile) {
return function (evt) {
var fd = new window.FormData();
fd.append('file', file);fd.append('filename', filename );
$.ajax({
url: "UploadFile.ashx",
type: 'POST',
processData: false,
contentType: false,
data: fd,
responseType: "json",
success: function (result) {
if (result.Operation == 'success') {
alert('File Uploaded Successfully');
}
},
error: function (fail) {

}
});
}
})(file);
reader.readAsDataURL(file);
}
}
function getFileName(fullPath) {
var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
var filename = fullPath.substring(startIndex);
if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
filename = filename.substring(1);
}
return filename;
}

</script>
<input type="file" id="mul_FileUpload" />

Create a generic Handeler

UploadFile.ashx:-
---------------
public class UploadFile : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
if (context.Request != null)
{
OperationDetail obj_Op = new OperationDetail();

if (context.Request.Files["file"] != null && context.Request.Files["file"].ContentLength > 0 )
{
HttpPostedFile postedFile = context.Request.Files["file"];
obj_Op.FilePath = Guid.NewGuid().ToString() + ".jpg";
postedFile.SaveAs(context.Server.MapPath("~/upload/temp/")+obj_Op.FilePath);
obj_Op.Operation = "success";
obj_Op.FileName = postedFile.FileName;
obj_Op.Index = Convert.ToInt32(context.Request["indexer"]);
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
string serobj_Op = javaScriptSerializer.Serialize(obj_Op);
context.Response.ContentType = "application/json; charset=utf-8";
context.Response.Write(serobj_Op);
}
}
else {

}
}

}
public class OperationDetail
{
public string FilePath{get;set;}
public string Operation { get; set; }
public string FileName { get; set; }
public int Index { get; set; }
}
 
Share this answer
 
v2
Comments
Abhilask kumar 29-Oct-13 2:44am    
tell me any jQuery file require?
You Can Do it using AJAX like following :

HTML :

<input id="fileupload" type="file" name="files[]" data-url="server/yourMethodToPost/">


jquery:

PHP
$(function () {
    $('#fileupload').fileupload({
        dataType: 'json',
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo(document.body);
            });
        }
    });
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900