PDA

View Full Version : Send full path of uploaded file to the server



northbridge
17 Jan 2012, 10:29 AM
I'm trying to move the selected file from one server to the other. How do I send the full path of the selected file to the .net mvc controller?

Here is my code


this.uploadForm = new Ext.form.Panel({
width: 400,
bodyPadding: 10,
fileUpload: true,
frame: true,
renderTo: Ext.getBody(),
items: [this.uploadFile],
buttons: [{
text: 'Upload',
scope: this,
handler: function () {
Ext.Ajax.request({
scope: this,
url: 'data/rest/Document/UploadDocument',
params: {
fileName: this.uploadFile.getValue()
// How do I send the the full path instead of just the file name
},
success: function () { }
}
}]
});

mitchellsimoens
17 Jan 2012, 11:14 AM
When you do a form.submit(), it is part of the file upload.

northbridge
17 Jan 2012, 11:56 AM
Thanks mitchellsimoens! I've not used form.submit before. I do not know how to read the value in my controller. I was debugging in firebug. I'm able to see just the filename under "post" , but not the full path. Here is what I've so far.


this.uploadForm = new Ext.form.Panel({ width: 400,
bodyPadding: 10,
fileUpload: true,
frame: true,
id: 'uploadForm',
renderTo: Ext.getBody(),
items: [this.uploadFile],
buttons: [{
text: 'Upload',
scope: this,
handler: function () {
var form = this.uploadForm.getForm();
if (form.isValid()) {
form.submit({
url: 'data/rest/Document/UploadDocument',
waitMsg: 'Uploading your photo...',
success: function (fp, o) {

}
});
}
}
}]

});


public ActionResult UploadDocument(string fileName) {
try
{
string test = "";
if (Request["filename"] != null) //This is always null
test = Request["filename"];
else
{
test = fileName;
}
return Json("Success");
}
catch (Exception ex)
{
return Json(ex.ToString());
}

}

northbridge
17 Jan 2012, 12:53 PM
I was able to read the form values, but still not getting the full path. Here is what I got


public ActionResult UploadDocument(string fileName) {
try
{
HttpPostedFileBase postedFile = Request.Files[0];
if (postedFile != null)
{
var fileName=postedFile.fileName;//The variable has the value abc.gif and not c:\temp\abc.gif
}


Any idea on how to get the full path? I'm testing this in Firefox

northbridge
17 Jan 2012, 1:55 PM
I figured. Thanks! Here is what I did



[NHibernateActionFilter]
public ActionResult UploadDocument(string fileName)
{
try
{
HttpPostedFileBase postedFile = Request.Files[0];
if (postedFile != null && postedFile.ContentLength > 0)
{
var newFile = Path.GetFileName(postedFile.FileName);
if (newFile != null)
{
var path = newFile;
//var path = Path.Combine("...", newFile);
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
postedFile.SaveAs(path);
}
}
return Json("Success");
}
catch (Exception ex)
{
return Json(ex.ToString());
}
}