I am using jqgrid, and I need to upload files from two different inputs using one ajax file upload call. I can upload one file using the fileElementId, but is there any way to upload two files simultaneously?
function uploadFiles(dfcId) {
$.ajaxFileUpload({
url: '/DigitalFileCabinet/UploadFile',
type: 'POST',
dataType: 'json',
secureuri: true,
async: false,
data: { id: dfcId },
fileElementId: "file1",
success: function (response, status) {
//success code
},
error: function (data, status, e) {
if (status === 'success')
return;
else
return alert('Failed to upload PDF!');
}
});
};
I have tried the following method, which uses an array to get two file elements at once and upload, but it didn't work.
https://www.programmersought.com/article/194562558/
Related
Goal:
Created client.ashx by copying existing and working product.ashx and renaming into client.ashx
Modified client.ashx to handle similar logic to product.ashx
I did the above since I don't know how to create an .ashx from scratch in visual studio community. I didn't see a file option for .ashx after the Add/New Item ...
The goal is to upload client images using client.ashx.
File product.ashx uploads product images.
My jscript call to product.ashx works as usual, although when I call the new client.ashx it seems to call product.ashx rather than client.ashx
Sample call to client.ashx
$.ajax({
type: "POST",
url: "/handlers/client.ashx",
data: formData,
global: false,
success: function (result) {
var response = JSON.parse(result);
var success = response.Success;
if (success) {
window.location.replace(window.location);
}
else {
ChangeStatusMessage(response.Message, false, true);
}
},
processData: false,
contentType: false,
error: function (result) {
ChangeStatusMessage("An unknown error occurred while trying to upload the image. Please reload the page and try again", false, true);
}
});
I want to be able to drag and drop files into multiple folders on a server, I am using jquery to pass to HttpHandler but I can't pass the save location to webhandler. I would like to send the path from jquery in the request is there a way to incluse that when the data for file transfer is passed.
$.ajax({
type: "POST",
url: "FileHandler.ashx",
contentType: false,
processData: false,
data: data,
success: function (result) {
alert(result);
},
error: function () {
alert("There was error uploading files!");
}
});
Maybe something like this:
$.ajax({
type: "POST",
url: "FileHandler.ashx",
contentType: false,
processData: false,
data: data,
filepath: document.getElementById("<%=listDrop.ClientID%>");
success: function (result) {
alert(result);
},
error: function () {
alert("There was error uploading files!");
}
});
and then retrieve the path in the webhandler to pass as save location?
I have tried this $.ajax({
type: "POST",
url: "FileHandler.ashx",
contentType:false,
processData: false,
data: {
data: newData,
filepath:JSON.stringify("~/uploads/")
},
success: function (result) {
alert('success');
},
error: function () {
alert("There was error uploading files!");
}
}); But I have question about the declaration of the data type for the files I will be uploading when creating the get and set in asp. filepath is a string but what data type are the files.
although i am answering from my mobile, i tried my best to keep the things intact and use ful. If required, Please format it accordingly.
I am not going to write entire code here, just algo.
1. Identify the parameter to be passed. If its only path of multiple folders then you can create an array of objects in javascript for the same. See tge example
var listOfFolder = new string[];
listOfFolder[0]="path of first dir";
:
:
listOfFolder[n]="path of nth dir";
var data = JSON.stringify(listOfFolder);
2. pass this data in data attribute of jquery ajax. 3. Grab this path in Process Request event and deserialize.
4. Do whatever you want.
I need to upload files and some other data, for which I used bellow code
HTML:
<intut type='file' id='file1'>
<intut type='file' id='file2'>
javascript:
var data = new FormData(),
categories = ['node.js','redis'],
roles = ['admin','HR'];
data.Append ($('#file1').Files[0].name, $('#file1').Files[0]);
data.Append ($('#file2').Files[0].name, $('#file2').Files[0]);
data.Append ('category', 'categories');
data.Append ('role', 'roles');
$.ajax({
url: baseaddress + 'DocRepo/GetAdminUploadData',
type: 'Post',
data: data,
cache: false,
dataType: 'json',
async: false,
contentType: false,
processData: false,
success: function (data) {
},
error: function (data) {
}
});
From here I have an action method in my MVC Controller (not webapi) GetAdminUploadData where I need to collect all data(files, category and roles) how can do it.
Assuming that you are only looking for how to get the non-file items (as the title says ... although is not mentioned in the body of the question) and that the server code is C# (as your note from 3-feb-15 hints) I'd say this question is a duplicate of and answered by: How can retrieve string formData js in c#.
Namely: HttpContext.Current.Request.Form["KEY"]
In PHP, I can call a page like this
var data = {
type: 'simple_data'
};
jQuery.ajax({
url: 'http://www.example.com/haha.php', //load data
type: "POST",
dataType: "xml",
data: data,
async: false,
success: loading_complete,
error: function (request, status, error) {
alert(error);
}
});
And in the PHP server side page, we catch it like
$type=$_POST['type'];
Pretty simple right! It gives back the XML info and GOAL.
Now I want to do it for ASP.NET pages in same way as PHP. I want to call the ASP.NET page like
var data = {
type: 'simple_data'
};
jQuery.ajax({
url: 'http://www.example.com/haha.aspx', //load data
type: "POST",
dataType: "xml",
data: data,
async: false,
success: loading_complete,
error: function (request, status, error) {
alert(error);
}
});
So how can I catch the data and extract values in ASP.NET. That means I want the functionality similar to this '$_POST['type']' in ASP.NET. I tried to search but nothing found yet or may be didn't find in the right direction. Can anyone please tell me how can I extract this data from this ajax call with XML??
You can use Request.Form["type"]
It is very easy. You need to pass the method name in your URL parameter like so:
jQuery.ajax({
url: 'http://www.example.com/haha.aspx/MethodName', //load data
type: "POST",
dataType: "xml",
data: data,
async: false,
success: loading_complete,
error: function (request, status, error) {
alert(error);
}
});
ASP.Net will know how to handle the web request and parse the data. You can write something on the .aspx page as simple as:
[WebMethod]
public static string MethodName(string type)
{
// do work with type
}
In my MVC3 app, I'm using $.ajax to call a method of type JsonResult to get data to be displayed:
function GetData(e) {
var ordersId = e.row.cells[0].innerHTML; //this is fine
$.ajax({
type: "POST",
url: "/Documents/GetDocumentData",
contentType: "application/json; charset=utf-8",
data: "{'id': '"+ordersId +"'}",
dataType: "json",
success: function (result) {
//load window
},
error: function (result) {
if (!result.success)
//show error
}
});
This is my Action:
[HttpPost]
public JsonResult GetDocumentData(string id)
{
//my code
return Json(new { success = true});
}
When im debugging on my development machine, it works fine. I deployed it to my test web server and i get '404 page not found dev/testwebsite/Documents/GetDocumentData' I should get this when debugging if something was wrong, but i dont. Why am I getting this error? Thanks
Your URL in the javascript is wrong, if that dev/testwebsite/Documents/GetDocumentData is the URL on the server.
You should be using #Url.Action() to automatically form those URLs in the cshtml page.
Example:
#Url.Action("actionName","controllerName" )
So, for your specific case, it would be:
#Url.Action( "GetDocumentData", "Documents" )
In the javascript, it would look like this:
function GetData(e) {
var ordersId = e.row.cells[0].innerHTML; //this is fine
$.ajax({
type: "POST",
url: '#Url.Action("GetDocumentData","Documents")',
contentType: "application/json; charset=utf-8",
data: "{'id': '"+ordersId +"'}",
dataType: "json",
success: function (result) {
//load window
},
error: function (result) {
if (!result.success)
//show error
}
});