Using dropzone need to add additional values - c#

I am using dropzone to upload files and images to the DB which works perfect, I generate the dropzone div's and call the dropzone jquery function.
In C# its been received by a WebMethod and files are being uploaded to the database.
Now I need to send several id with however I would like to avoid to implement a ajax call to send these id's. After read the documentation on dropzonejs I could not find a simple solution to do this.
My WebMethod does not accept parameter for now but when I have a good way to implement this on client I can add these to WebMethod.
Did I miss something or Am I only able to do this with ajax?
In short looking for the "data:" object within dropzone as in ajax

I am not sure if I understood your question correctly, the formData is available before each files are sent.
From Dropzone.js. Sending: Called just before each file is sent. Gets the xhr object and the formData objects as second and third parameters, so you can modify them (for example to add a CSRF token) or add additional data.
Sample usage:
sending: function(file, xhr, formData) {
formData.append("test",$('#test').val());
},

Related

POST method to send RAW TEXT data and get HTML Response using Jquery AJAX Web Method and check with POSTMAN

I'm Trying to do a POST Request using RAW TEXT.
I want to send POST Request from POSTMAN like, in body Section I've selected RAW(from radio buttons) and Text(from drop-down) and pass below simple string.
01JAINAM120112356598
Now, when we click on send in Postman, Response must come in HTML string "01" on success and "00" on error.
[Type of Request & Response I Need] https://imgur.com/bC26Pw7 (In photo URL is of .php page, but I'll pass my asp.net c# page (.aspx)).
I need to use JQuery ajax code to pass request & get proper response.
Something like,
$.ajax({
url: "RFID.aspx/saveRFIDData",
type: "POST",
dataType: "text",
success: function () {
},
error: function (response) {
alert("Error")
}
});
But, when I call above URL as post method as same shown in Image above, my WebMethod in code behind is not called.
Instead, when I call same ajax call for JSON, my WebMethod is been called.
But, I don't want to pass JSON.
So, Can anyone help me to get it done with "dataType : text" & get HTML string response.
Note : If someone didn't Understand my question. Do comment first, to be more clear rather than DEVOTE this question
It seems to be "Unsupported Media Type" issue and status may be 415. Add supported media type, that might work.

pdf.js how to pass a file response object in c#

so I am using the excellent pdf.js tool and it works great. However, I'd like to pass it a response object that has the PDF file stream from Amazon S3 over to the viewer of pdf.js.
In the demo I see it calls it like this:
=/pdf/web/viewer.html?file=%2FmypdfFile.pdf
However, looking in viewer.html or pdf.js or any of its files, I cannot see where on earth its using the ?file parameter that is passed on the URL. I'd like to replace it with something where I can pass it a response item and it will load up the viewer.html.
I'd like to do something like this:
(pseudo code sorta)
request = S3.GetObjectRequest(bucket, key);
using GetObjectResponse response - client.getobject(request);
openPDFViewer (response);
Is that doable? response would contain the file, i.e. I can say
response.WriteResponseToFile("c:\mypdf.pdf")
and I get the file out.
I cannot see where on earth its using the ?file parameter that is
passed on the URL. I'd like to replace it with something where I can
pass it a response item and it will load up the viewer.html.
If you look into viewer.js, there is this pdfViewOpen method having following parameter:-
pdfViewOpen(url, id, scale, password,pdfDataRangeTransport, args)
The string url that you pass after file= is passed in as url parameter of this method. You can change inside that method what u want to do with it.
From there you might want to look at PDFJS.getDocument() method of pdf.js file, this follows to fetchDocument method from where its taken care by messageHandler.
If you want to handle all that by yourself, you can intercept it before. There are already several examples on SO. Here is link to one:-
How to get byte-array data from servlet to pdf.js
IIRC S3 files have URL references so you don't have to 'preload' the pdf on the server side. Just render out the URL to have the file=[urlencoded path to s3 file]

Uploading files by dropzone.js give error, Request format is invalid: multipart/form-data;

I am using dropzone to being able to upload multiple files to a Webservice, however I keep on getting error:
System.InvalidOperationException: Request format is invalid: multipart/form-data; boundary=----------WmBuH1mgT5UyAi5sEK0qnm.
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
this is my C# function
public bool InvokeTest(byte[] binaryarray, string docname)
As I am trying to avoid Page_Load and try to make it work without a Form submit and only with a jQuery function as:
$('#dropzone').dropzone({
url: '_upload/uploader.asmx/InvokeTest',
enctype: 'multipart/form-data',
maxFilesize: 1,
paramName: 'photos',
addRemoveLinks: true,
enqueueForUpload: false,
});
my form submit looks simple:
<form id="Form1" action="_upload/uploader.asmx/InvokeTest" method="post" enctype="multipart/form-data" runat="server" class="dropzone">
My questions:
Is it possible to use dropzone without "form" and post it direct through the jQuery and avoid my nasty error on the webservice....
Looking forward to any idea's and or suggestions,
Thanks
Dropzone.js does work well in conjunction with a webservice & more specifically, it can be used in scenarios, where
you are using a form to post to a WebServiceURL
you are looking to upload multiple images
, which, judging by your question, you were originally looking to do.
Here is a detailed example of exactly such a scenario. Although in the example ServiceStack (rather than WCF) is being used as WebService: [ Uploading images using ServiceStack and Dropzone ]
This suggests that your WebService c# function is not set up correctly to handle the post request.
Another important dropzone configuration is to use parallelUploads = 1. I have seen this being an issue if multiple uploads are looking to access the same memoryStream on the server-side implementation.
$('#dropzone').dropzone( {
parallelUploads: 1,
uploadMultiple: true,
autoProcessQueue: true
});

Return tabledata as file with jquery and c# mvc

I would like to use jquery to send Table-data to the server and then get a data.csv file to download
if i navigate to the url in my browser like this:
http://localhost:49400/File/Csv/?Text=qweerty&Filename=asdf
i get promted with a file to download.
This is the action im calling:
public FileResult Csv(FileModel fileModel)
{
return File(Encoding.UTF8.GetBytes(fileModel.Text), "text/plain", string.Concat(fileModel.Filename, ".csv"));
}
and my javascript looks like this:
$("table").click(function () {
$.ajax({
type: "POST",
url: "http://localhost:49400/File/Csv/",
data: {"Text": "qwerty", "Filename": "asdf"}
})
});
The response in firebug is containing the data, but i would like it to ask the user if it wants to download it, is this possible?
You cannot use AJAX to download a file.
Instead, you can just set location to a URL pointing to the file.
As long as the server returns a downloadable file (with a Content-Disposition header), the browser will show a Save dialog and will not replace the page.
If you want to download the file from a POST, you could make a hidden <form> that sends the POST, then submit() the form using Javascript.
you can't deliver file content through ajax call, perhaps find a way to construct URL with your table data as query string and stream the file that way or post-back the whole page and response as a stream

C# httpwebrequest on javascript

As some of you may know, you are able to POST with C#. This means you can "push" buttons on a website with webrequest/response. Now there are also buttons on sites which work with javascript, they start like:
(function($j){
$j.data(document, 'maxPictureSize', 764327);
share_init();
})(jQuery.noConflict());
Is there any solution you can make those function calls in C# with like httprequests or any other kind of library?
I'm assuming you have a program that wants to manipulate the server "back end" for a web page by making the server think that someone pushed a button that POSTs, and sending the data that the web page would include with its POST.
The first tool you need is Microsoft Network Monitor 3.3, or another network packet tracing tool. Use this to look at the POST from the real web page. NetMon (at least) decomposes the packet into the HTTP pieces and headers, so you can easily see what's going on.
Now you will know what data the real POST is sending, and the URL to which it is sending the data (with any possible "query string" - which is unusual for a POST).
Next you need to write C# to create the same sort of POST to the same URL. It seems that you already know about HttpWebRequest/HttpWebResponse so I won't explain them in detail. You may have noticed in your NetMon trace that the Content-Type header was application/x-www-form-urlencoded. This is most often data from an HTML form which is URL-Encoded (like the name), so you need to URL-Encode your data before POSTing it, and you need to know the size of the encoded data for the Content-Length. HttpUtility.UrlEncode() is one method to use for this encoding.
Once you think you have it, try it and use NetMon to inspect your POST request and the response from the server. Keep going until you have duplicated what the mystery web page is doing.
Ok use webBrowser form to load the page:
webBrowser.Navigate( url );
then save the contents of the web broweser form to a file or a string:
File.WriteAllText(#"c:\test\ajax_test.txt", webBrowser1.Document.Body.Parent.OuterHtml, Encoding.GetEncoding(webBrowser1.Document.Encoding));
now if you look to the txt file it should have the html tags you look for.
Even when using JavaScript to do a POST there is a POST somewhere in the JS which works the same way as button submit. You just have to dig to the place where the JS code posts and see how it does it. Then craft the same post in C#.
Take for example ASP.NET's own __doPostBack function
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
You can see that it digs for the form sets several values for input fields and does a submit. Basically you need to fill the same values for the inputs and submit the same form and you've got the JS submit done yourself.
You need to capture the requests and headers those buttons are sending and simulate them with HttpWebRequest. You could also take a look at WatiN if you want to automate user actions on web sites.

Categories

Resources