I would like to have members to be able to upload a photo of their selfs. I made a membertype with a upload datatype. In the back end, this works good. Editors can add / change / delete an image easily. But now I would like to use that same upload control in the frontend, available for members. I guess I need to use umbraco.editorControls.uploadField? But how can I use this control?
The uploadField DataType is really just a wrapper for the FileUpload control (well, actually HtmlInputFile, as it's a hold over from .NET 1.1 when FileUpload didn't exist).
Really all you need to do is put your own FileUpload control on the form. To get it to save in the 'umbraco standard' folder path you need to upload to the /media/<property id>/ folder.
It's just a matter of uploading a file using standard ASP.NET and then writing the path into the Value property of the property.
And here is the code-behind to process the request and create an image:
// submit handler
protected void submit_Click(object sender, EventArgs e)
{
int mediaId = SaveFile(UserProfileImage,{ID of parent in media tree}).Id;
// here you can save the media ID to the member that you are editing by
// simply setting the member property...something like:
Member m = Member.GetCurrentMember();
m.getProperty("memberProfilePic").Value = mediaId;
}
// Helper Method to upload and store your image in the media tree
protected Media SaveFile(FileUpload uploadControl, int mediaParent)
{
string mediaPath = "";
Media m = null;
if (uploadControl.PostedFile != null)
{
if (uploadControl.PostedFile.FileName != "")
{
// Find filename
_text = uploadControl.PostedFile.FileName;
string filename;
string _fullFilePath;
filename = _text.Substring(_text.LastIndexOf("\\") + 1, _text.Length - _text.LastIndexOf("\\") - 1).ToLower();
// create the Media Node
m = Media.MakeNew(
filename, MediaType.GetByAlias("image"), User.GetUser(0), mediaParent);
// Create a new folder in the /media folder with the name /media/propertyid
string mediaRootPath = HttpContext.GetGlobalResourceObject("AppSettings", "MediaFilePath") as string;
string storagePath = mediaRootPath + m.Id.ToString();
System.IO.Directory.CreateDirectory(storagePath);
_fullFilePath = storagePath + "\\" + filename;
uploadControl.PostedFile.SaveAs(_fullFilePath);
// Save extension
string orgExt = ((string)_text.Substring(_text.LastIndexOf(".") + 1, _text.Length - _text.LastIndexOf(".") - 1));
orgExt = orgExt.ToLower();
string ext = orgExt.ToLower();
try
{
m.getProperty("umbracoExtension").Value = ext;
}
catch { }
// Save file size
try
{
System.IO.FileInfo fi = new FileInfo(_fullFilePath);
m.getProperty("umbracoBytes").Value = fi.Length.ToString();
}
catch { }
// Check if image and then get sizes, make thumb and update database
if (",jpeg,jpg,gif,bmp,png,tiff,tif,".IndexOf("," + ext + ",") > 0)
{
int fileWidth;
int fileHeight;
FileStream fs = new FileStream(_fullFilePath,
FileMode.Open, FileAccess.Read, FileShare.Read);
System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
fileWidth = image.Width;
fileHeight = image.Height;
fs.Close();
try
{
m.getProperty("umbracoWidth").Value = fileWidth.ToString();
m.getProperty("umbracoHeight").Value = fileHeight.ToString();
}
catch { }
// Generate thumbnails
string fileNameThumb = _fullFilePath.Replace("." + orgExt, "_thumb");
generateThumbnail(image, 100, fileWidth, fileHeight, _fullFilePath, ext, fileNameThumb + ".jpg");
image.Dispose();
}
mediaPath = "/media/" + m.Id.ToString() + "/" + filename;
m.getProperty("umbracoFile").Value = mediaPath;
m.XmlGenerate(new XmlDocument());
}
}
// return the media...
return m;
}
apart from the usual references to the Umbraco dll(s) for the Media class you do not need any additional references, the FileUpload control is standard ASP.Net.
Upload datatype / member uploading image
I would like to have members to be able to upload a photo of their selfs. I made a membertype with a upload datatype. In the back end, this works good. Editors can add / change / delete an image easily. But now I would like to use that same upload control in the frontend, available for members. I guess I need to use umbraco.editorControls.uploadField? But how can I use this control?
Thanks in advance, Oskar
I'd like to see how to go about this as well...
The uploadField DataType is really just a wrapper for the FileUpload control (well, actually HtmlInputFile, as it's a hold over from .NET 1.1 when FileUpload didn't exist).
Really all you need to do is put your own FileUpload control on the form. To get it to save in the 'umbraco standard' folder path you need to upload to the /media/<property id>/ folder.
It's just a matter of uploading a file using standard ASP.NET and then writing the path into the Value property of the property.
I'd like to see this as well...
#slace
It sound easy, but not for non-developers like myself - does someone have som sample code how to do this?
best regards,
hundebol
Here is one way of doing this.
UserControlToUploadFile.ascx
And here is the code-behind to process the request and create an image:
HTH,
Nik
Thanks for the code, I was looking for something like this.
Do I need to add any references to make this work?
Hi Ernst,
apart from the usual references to the Umbraco dll(s) for the Media class you do not need any additional references, the FileUpload control is standard ASP.Net.
Cheers,
Sascha
Sorry to bring up this old tread, I tried the code above but it’s giving me an
“Access to the path '1112' is denied.” Error (the path changes each time)
But I have no problem setting other member values, like first and last name.
It’s no problem uploading when logged in with admin in umbraco, and shouldn't this piece of code create the picture with the same privileges:
User.GetUser(0) or new umbraco.BusinessLogic.User(0) ??
Any help would be appreciated
found the solution here: http://our.umbraco.org/forum/developers/api-questions/3477-Creating-Media-Programatically
is working on a reply...