Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi every one , I am trying to save image path in virtual folder using jquery and handler class so that it doesnt postback the page below are my codes please rectify it and if possible correct it

JavaScript
$.ajax({
        dataType:'json',
        type: 'POST',
        url: 'Handler1.ashx?imagepath="'+file+'"&filename=cool',
        data: { 'Path': '"' + file + '"' },
        success: function (response) {
            alert("posted");
        }
});

this is for sending param to the Handler Class using Jquery

C#
public void ProcessRequest(HttpContext context)
        {
            var file = context.Request.QueryString["imagepath"].Replace("/", "\\");
            string savedFileName = "";
            string Path = context.Request["Path"];
            savedFileName = context.Server.MapPath("Image");
            context.Request.Files[0].SaveAs(savedFileName);
        }




C#
public bool IsReusable
        {
            get
            {
                return false;
            }
        }

Please help me in saving the image in the virtual folder Image in my project the difficulty I am having is that
C#
context.Request.Files[0].SaveAs(savedFileName);
is showing me the error
Quote:
Index was out of range. Must be non-negative and less than the size of the collection.
Any help will be appreciated! thanks in advance..
Posted
Updated 2-Jan-22 5:44am
v7
Comments
Sergey Alexandrovich Kryukov 11-Oct-13 17:43pm    
Please format code properly, with "pre" tags. I tried to fix, it, you can see how the format looks if you click "Improve question".
—SA

These code will help everyone to get file uploaded into folder without postback...
The only thing left to get the response from handler that the File has been uploaded
any further help will be appreciated.

(Jquery)

JavaScript
function clked() {
    var fileUpload = $("#up").get(0);
    var files = fileUpload.files;
    var src = $("#up")[0].value;
    var name = src.split('').reverse().join('');
    var revercedName = name.split('\\');
    var files_name=revercedName[0].split('').reverse().join("");
    var data = new FormData();
    for (var i = 0; i < files.length; i++) {
        data.append(files[i].name, files[i]);
    }
    $.ajax({
        dataType: 'json',
        type: 'POST',
        processData: false,
        contentType: false,
        url: 'Handler1.ashx?imagename=' + files_name + '',
        data: data,
        success: function (response) {
            alert("posted");
        }
    });
}


C# code in Handler

C#
public void ProcessRequest(HttpContext context)
       {
           var file = context.Request.QueryString["imagename"];
           //var file = context.Request.QueryString["imagepath"].Replace("/", "\\");
           string savedFileName = context.Server.MapPath("Image/" + file);
           context.Request.Files[0].SaveAs(savedFileName);

       }

       public bool IsReusable
       {
           get
           {
               return false;
           }
       }
 
Share this answer
 
v4
Hey there,

You need to make sure, you have done the things described below:

1- Add enctype="multipart/form-data" property in the form tag of your page.

2- I create an example to see how it works, follow it in your own code:
aspx:
ASP.NET
<asp:fileupload runat="server" id="file" onchange="upload();" />

Jquery:
JavaScript
function upload() {
            var fileUpload = $("#file").get(0);
            var files = fileUpload.files;

            var data = new FormData();
            for (var i = 0; i < files.length; i++) {
                data.append(files[i].name, files[i]);
            }
            $.ajax({
                dataType: 'json',
                type: 'POST',
                processData: false,
                contentType: false,
                url: '/AjaxFileUpload/AjaxUpload.ashx',
                data: data,
                success: function (response) {
                    alert("posted");
                }
            });
        }


Replace the URL with your own URL of handler and the ID of the FileUpload control, and you are good to go.
[Edit]Use latest JQuery library in your application to make it work.[/Edit]

Cheers.

Let me know if it helps.

Azee...
 
Share this answer
 
v4
Comments
leexb 12-Oct-13 5:17am    
i try to run your code ,still the same error.
context.Request.Files.count=0 in AjaxUpload.ashx file
Azee 12-Oct-13 5:22am    
Can you post the your updated code (jquery and aspx), so I can reproduce it here and see whats wrong.
leexb 12-Oct-13 5:33am    
aspx page:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

<script type="text/javascript">
function upload() {
var fileUpload = $("#file").get(0);
var files = fileUpload.files;

var data = new FormData();
for (var i = 0; i < files.length; i++) {
data.append(files[i].name, files[i]);
}
$.ajax({
dataType: 'json',
type: 'POST',
processData: false,
contentType: false,
url: 'UploadFile.ashx',
data: data,
success: function (response) {
alert("posted");
}
});
}
</script>
</head>
<body>
<form id="form" action="" runat="server" enctype="multipart/form-data">
<asp:fileupload runat="server" id="file" onchange="upload();" />
</form>
</body>
</html>

UploadFile.ashx:

public class UploadFile : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
int count = context.Request.Files.Count;
}

public bool IsReusable
{
get
{
return false;
}
}
}
Azee 12-Oct-13 6:06am    
Hey, download the latest JQuery.js and replace <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> with that one. It'll work.

let me know.
Member 10323752 13-Oct-13 1:24am    
Hai thanks I have done file Saving with your help... Thanks I will post the updated codes now so that everyone can have it. Now atleast postback will not happen.
The goal of this problem is to find the smallest integer in a list of numbers.
To help you with this task, please write a function called min() that finds and returns the smallest amongst two integers (be sure to also write a prototype for this function). The function thus takes two integers as input and returns the smallest of the two. This function will use an if statement with a condition that contains either "less than" or "greater than".
Next, please use min() in your main function to work your way through an entire list of numbers in order to find its minimum. The first number you read gives the number of elements in the list of integers under consideration. You will then read the integer values, using min() to keep only the smallest integer read at each step. In the end, please print out the smallest integer in the list.
 
Share this answer
 
Comments
Dave Kreskowiak 2-Jan-22 12:07pm    
You posted your homework assignment as an answer to an eight year old question. It'll be ignored here.

Go to the Quick Answers menu above and click "Ask a question" to post your own question, BUT, you have to actually ask a question. Just posting your homework assignment won't get you any answers.

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