PDA

View Full Version : How to upload files?



Elfayer
5 Sep 2012, 4:34 AM
Hi,

I adapted the example usage of : http://docs.sencha.com/ext-js/4-1/#!/api/Ext.form.field.File

Here I am :


Ext.define('EXC.view.uploadWindow', {
extend: 'Ext.window.Window',
alias: 'widget.uploadWindow',

title: 'Upload',
width: 400,
draggable: false,
resizable: false,
items: {
xtype: 'form',
bodyPadding: 10,
frame: true,
items: [{
xtype: 'textfield',
fieldLabel: 'Name',
labelWidth: 50,
allowBlank: false,
anchor: '100%'
}, {
xtype: 'filefield',
fieldLabel: 'File',
labelWidth: 50,
allowBlank: false,
anchor: '100%',
buttonText: 'Browse...'
}],

buttons: [{
text: 'Upload',
handler: function () {
var form = this.up('form').getForm();
if (form.isValid()) {
form.submit({
url: 'file-upload.php',
waitMsg: 'Uploading your file...',
success: function (fp, o) {
Ext.Msg.alert('Success', 'Your file "' + o.result.file + '" has been uploaded.');
}
});
}
}
}]
}
});

But I don't understand the part with the upload button handler. What do I have to put in the "url"? How does that work?

I would like to upload .pdf, .xls, .ppt and .csv files. So I have to add a new file in the file table of my database, but I also need to copy the file uploaded in a directory of the server. How do I do that?

Thanks in advance !

bomslang
5 Sep 2012, 4:36 AM
Explained here :-

## http://stackoverflow.com/questions/2514937/file-upload-using-ext-js

(http://stackoverflow.com/questions/2514937/file-upload-using-ext-js)If you want to go for MVC app, Loiane Groner has got it implemented. Check it out here
http://java.dzone.com/articles/extjs-4-file-upload-spring-mvc

Tim Toady
5 Sep 2012, 5:27 AM
The url is the location your upload data is sent. In this particular situation it will be a file in the same directory as your index.php called file-upload.php. You will need to specify the name property for the field as is done in the example you linked. In their case it is 'photo'. It gets uploaded to a tmp directory in php and to get the path you would do something like


$filepath = $_FILES[ 'photo' ][ 'tmp_name' ];


You could then do whatever you want with the file on the serverside. You could use something like move_uploaded_file (http://php.net/manual/en/function.move-uploaded-file.php) to put it in the directory you want.

Elfayer
5 Sep 2012, 5:39 AM
Thank you both,
I'm reading your links.

@Tim Toady : I don't understand a thing, I don't have any php file in my code. I only have a Index.cshtml in a View/Home/ directory.

Tim Toady
5 Sep 2012, 5:53 AM
I was talking about php because the example you had was calling a php script. You will need to handle saving the file in your serverside language. In your case it appears to be c#/asp.


Thank you both,
I'm reading your links.

@Tim Toady : I don't understand a thing, I don't have any php file in my code. I only have a Index.cshtml in a View/Home/ directory.

Elfayer
5 Sep 2012, 6:46 AM
I have only copy/paste it, my mistake ! ;)

I corrected it :
url: '/Views/Home/fileUpload.cshtml',

I read the links of bomslang (http://www.sencha.com/forum/member.php?407675-bomslang). The first only explain how to insert a line in a database and in php code. I think that won't be difficult, and I didn't understand a thing of the second link. Why do I need an HTML page? What is a multipart file? And she is using Spring MVC 3. I'm using C#.

It's a bit jumbled in my head. I'm a novice and I don't know many things about network. If someone could explain me the steps and how does it work, that would help me a lot.

Tim Toady
5 Sep 2012, 7:48 AM
Multipart is just a content type for submitting forms that contain files. You don't really need to worry about it. You need some sort of page on your server to handle saving the file or writing to your database. I am not familiar with c# in a web application context, so hopefully someone who is familiar can guide you or you can find it on Google.