How to see the request variables or files sent from the phonegap to MVC asp.net Controller - iphone

I have done coding
public JSONResult media(HttpPostedFileBase file)
{
=====done with some code===
}
Here i m getting the file as null always.
Note:
file is the file submitted from the Phonegap's JSON method.
My question is :
Is there any mechanism for the decoding the encoded multipart file before reading?

Got it :)
Use the request params for getting the file input and changed the method as
public JSONResult media()
added request.files and stored in HTTPcollectionfile base and got the same file information posted in the PhonegapAPI

Related

Empty MultipartFile[] when sending files from Vue to SpringBoot controller

I'm doing a program that will help me to make monthly reports and I stuck at uploading photos which I need for one kind of the reports. For some reason, it doesn't get an array in the controller.
I use Springboot RestController at the backend and Vue with BootstrapVue and vue-resource on the other side.
index.html (BootstrapVue):
<b-form-file
v-model="photos"
accept="image/*"
multiple
placeholder="..."
></b-form-file>
<b-button #click="uploadPhotos">Upload</b-button>
inside vuemain.js:
data: {
photos: null,
},
methods: {
uploadPhotos(){
var formData = new FormData();
formData.append("photos", this.photos);
this.$http.post('reports/photo', formData).then(result => {
...
})
}, ...
inside Controller:
#PostMapping("/photo")
public void addPhoto(#RequestParam("photos") MultipartFile[] photo) {
System.out.println(photo.length); // shows 0
}
what I see inside Params at browser:
XHRPOSThttp://localhost:8080/reports-maker/reports/photo
[HTTP/1.1 500 326ms]
Request payload
-----------------------------4469196632041005505545240657
Content-Disposition: form-data; name="photos"
[object File],[object File],[object File],[object File]
-----------------------------4469196632041005505545240657--
​
So for some reason at this point #RequestParam("photos") MultipartFile[] photo it's empty array. But if I change it to just one photo like this: #RequestParam("photos") MultipartFile photo and send one from js: formData.append("photos", this.photos[0]); everything works nicely and photo gets uploaded to the server.
It's my first experience with Vue and to be honest I don't want to go deep into JS learning, so probably there is some silly mistake somewhere. Any way I can use a loop in JS method to upload them one by one, but that would be so ugly. I hope there is a better way to do it (without any additional JS libraries of course).
If you use axios then you should add header
var headers = {
'Content-Type': 'multipart/form-data',
};
axios.post(
'reports/photo',
formData,
{
headers: headers,
}
)
...
to be able send files to the server.
I agree, sending files in separate requests one by one is very "ugly", but I also don't like the idea of not using the mapping resources of Spring Boot, having to send all files with different names (seems disorganized) and having to work with MultipartHttpServletRequest, but there is a simple solution for this: Ian's answer to this question (not realy related to Vue.js, but useful) worked for me:
In order for Spring to map items in a request to a list, you need to provide the same name (in the FormData.append calls) for each item when appending to the form data. This allows Spring to effectively see the request as name=value1&name=value2&name=value3 (but obviously in the form of form data). When Spring sees the same key ("name") multiple times, it can map the values into a collection.
In your .vue file, append the photos with the same name:
for (let i = 0; i < this.photos.length; i++) {
formData.append("photos", this.photos[i]);
}
And in your Controller:
#PostMapping("/photo")
public void addPhoto(#RequestParam("photos") MultipartFile[] photo) {
System.out.println(photo.length); // Should be greater than 0 now
}
Note:
I used Vue Axios to consume my API and manually added the Content-Type: multipart/form-data header. Make sure its in your request header list.
I found an acceptable solution here https://stackoverflow.com/a/33921749/11508625 Rossi Robinsion's code works nicely. At least it looks better than sending files in separate requests one by one.
The answer is based on using getFileNames() which helps to iterate on files inside a request even if they are not in the array.

How to verify file type for multipart/form data upload in vertx?

I have a multipart/form with file upload in my Vert.x web application.
The issue I am running into is that I can only seem to validate the type of the uploaded file after having the request go through Vertx's BodyHandler.
Now I can validate the type after it went through, but the BodyHandler has already uploaded the file at this point in time. On a similair issue it was stated that you should check the content-type with your own handler before the BodyHandler, but this always returns an empty formAttributes.
I've tried creating my own handler:
public void handle(RoutingContext context) {
context.request().setExpectMultipart(true);
MultiMap attributes = context.request().formAttributes();
System.out.println(attributes);
context.next();
}
But attributes is always empty, so I can't verify the content-type of the file uploaded.
After it passes through the BodyHandler into my other handler it works fine:
MultiMap attributes = context.request().formAttributes();
Set<FileUpload> uploads = context.fileUploads();
for (FileUpload file : uploads
) {
System.out.println(file.contentType());
// This returns image/jpeg
}
context.response().end();
But as I stated above, the file is already uploaded when it has passed through the BodyHandler.
Here is the code for the handlers:
router.post("/api/someendpoint").handler(new FileTypeHandler());
router.post("/api/someendpoint").handler(BodyHandler.create()
.setUploadsDirectory("static/images")
.setBodyLimit(MB * 1));
router.post("/api/someendpoint").handler(new EndPointHandler());
How can I verify the type of a file being uploaded before it passes through the BodyHandler, so that I can reject any other file upload except an image?

How to get the file content type from the REST service?

I have a REST service to upload a file, is there a way to know what content type I might be getting? The user using my REST service might upload a PDF, Word or Excel document.
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Path("/details/documents")
public Response uploadDocument(#FormDataParam("file") File documentContent,
#FormDataParam("documentName") String documentName)
throws Exception {
//more implementation code here to upload the file.
}
Instead of File as a method parameter, you can use FormDataBodyPart, and then call getMediaType(). If you want to get the part's body as a File, just use getValueAs(File.class) or if you want the input stream just use InputStream.class.
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Path("/details/documents")
public Response uploadDocument(#FormDataParam("file") FormDataBodyPart part) {
MediaType mediaType = part.getMediaType();
File file = part.getValueAs(File.class);
}

Wowza secure Apple HTTP Live Streaming (AES-128 - external method). Player is not making the key request

I have been working on Wowza Streaming Server and while trying to secure Apple HTTP Live Streaming using AES-128 - external method I am encountering below problems :
External AES-128 method of encryption is not working for .smil files present in the sub-folder of the application's source directory. I tried to achieve it by putting the [my-stream].key in [install-dir]/keys and [install-dir]/keys/[sub-folder-name] but both the scenarios failed for me to achieve this.
playlist url is :- [wowza-server-ip]:[port]/[application-name]/[applcation-instance-name]/smil:[sub-folder]/demo.smil/playlist.m3u8
In case of mp4s present in the application's source path, the player is not calling the key url.
The sequence of calls made by the player are :-
[wowza-server-ip]:[port]/crossdomain.xml
[wowza-server-ip]:[port]/[application-name]/[applcation-instance-name]/[stream-name]/playlist.m3u8
[wowza-server-ip]:[port]/[application-name]/[applcation-instance-name]/[stream-name]/chunklist_w[wowza-session-id].m3u8
[web-server-ip]:[port]/crossdomain.xml
After this player is not calling the "key request uri" as it was supposed to call. The calls are going properly when I am using the internal AES-128 method of Encryption.
My chunklist_w[wowza-session-id].m3u8 is
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:12
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-KEY:METHOD=AES-128,URI="http://[web-server-ip]:[port]/SimpleWebServlet/key.jsp?wowzasessionid=[session-id]"
#EXTINF:9.52,
media_w[session-id]_0.ts
#EXTINF:10.4,
media_w[session-id]_1.ts
[streamname].key file in [install-dir]/keys folder is
cupertinostreaming-aes128-key: DE51A7254739C0EDF1DCE13BBB308FF0
cupertinostreaming-aes128-url: http://[web-server-ip]:[port]/SimpleWebServlet/key.jsp
jsp file to return the key is key.jsp
<%# page import="java.util.*,java.io.*" %>
<%
boolean isValid = true;
if (!isValid)
{
response.setStatus( 403 );
}
else
{
response.setHeader("Content-Type", "binary/octet-stream");
response.setHeader("Pragma", "no-cache");
String keyStr = "DE51A7254739C0EDF1DCE13BBB308FF0";
int len = keyStr.length()/2;
byte[] keyBuffer = new byte[len];
for (int i=0;i<len;i++)
keyBuffer[i] = (byte)Integer.parseInt(keyStr.substring(i*2, (i*2)+2), 16);
OutputStream outs = response.getOutputStream();
outs.write(keyBuffer);
outs.flush();
}
%>
If anybody has encountered the similar problem or has successfully implemented the external aes-128 method of wowza, kindly put some light on the issues mentioned above.
EDIT 1
Kindly ignore the 2nd point as after further analysis I found out that there is some issue with the jboss delivering the key, once it delivers the crossdomain xml to the player.
For reference to this problem kindly check : Can I call two crossdomain.xml from two different servers from my flash player?
EDIT 2
Apologies for the typo in my first point. It should be .smil rather than .mp4, I have corrected the same in my first point
I recently tried out HLS with AES128 and it worked fine. My key file was in [wowzadir]/keys/mystream.key. Looks like it is your player that does not do something right here. Which player are you using?
You can try to use wget to download some chunks and you can inspect them with VLC for example to see if the encryption was applied.

GWT request to download file

I know it's not possible to send an ajax request or use GWT's RequestBuidler to send a request for a file download; needing a form to do it but how do I get a reference to the response when it returns with lets say an error.
The request i send is for a file download but if the file download is too big the Java servlet responds with an error, how do i get a reference to this error to handle it appropriately on the GWT side.
You can add a FormPanel.SubmitCompleteHandler to the form and parse the results in onSubmitComplete().
With extGWT you can parse the HTML response to know the HTML error code
For instance
com.extjs.gxt.ui.client.widget.Component.addListener(Events.Submit, new Listener<FormEvent>()
{
public void handleEvent(final FormEvent event)
{
String htmlResponse = event.getResultHtml();
(...)
}
});