It looks like you're new here. If you want to get involved, click one of these buttons!
$(window).load(
function() {
$(\"#<%=fileUpload.ClientID %>\").fileUpload({
'uploader': '<%=uploader %>',
'buttonImg': '<%=buttonImg %>',
'cancelImg': '<%=cancelImg %>',
'folder': '<%=Folder %>',
'script': '<%=script %>',
'fileExt': '<%=FileExt %>',
'fileDesc': '<%=FileDesc %>',
'sizeLimit': '<%=SizeLimit %>',
'scriptData': <%=GenerateScriptData() %>,
'auto': <%=Auto.ToString().ToLower() %>,
'width': <%=width %>,
'height': <%=height %>,
'onSelect': <%=OnSelect %>,
'onComplete': <%=OnComplete %>,
'onError': <%=OnUploadError %>,
'multi': <%=Multi.ToString().ToLower() %>
});
}
);
<div style=\"float: left;\"><asp:FileUpload ID=\"fileUpload\" runat=\"server\" /></div>
#region Private Properties
string folder = \"\";
string fileExt = \"\";
string fileDesc = \"\";
string sizeLimit = \"2620000\";
Dictionary<string, string> scriptData;
bool auto = true;
string onSelect = \"function(event, queueID, fileObj) { }\";
string onComplete = \"function(event, queueID, fileObj) { }\";
string onError = \"function(event, queueID, fileObj, errorObj) { }\";
bool showSizeLimit = false;
string sizeLimitClass = \"\";
bool showPreview = false;
string previewImage = \"\";
bool multi = false;
#endregion
#region Public Properties
public string uploader;
public string buttonImg;
public string cancelImg;
public string script = \"\";
public string Folder { get { return folder; } set { folder = value; } }
[UrlProperty]
public string Script { get { return script; } set { script = value; } }
public string FileExt { get { return fileExt; } set { fileExt = value; } }
public string FileDesc { get { return fileDesc; } set { fileDesc = value; } }
public string SizeLimit { get { return sizeLimit; } set { sizeLimit = value; } }
public Dictionary<string, string> ScriptData
{
get { if (scriptData == null) scriptData = new Dictionary<string, string>(); return scriptData; }
set { scriptData = value; }
}
public bool Auto { get { return auto; } set { auto = value; } }
public string OnSelect { get { return onSelect; } set { onSelect = value; } }
public string OnComplete { get { return onComplete; } set { onComplete = value; } }
public string OnUploadError { get { return onError; } set { onError = value; } }
public int width = 160;
public int height = 22;
public string FileUploadID { get { return fileUpload.ClientID; } }
public bool ShowSizeLimit { get { return showSizeLimit; } set { showSizeLimit = value; } }
public string SizeLimitClass { get { return sizeLimitClass; } set { sizeLimitClass = value; } }
public bool ShowPreview { get { return showPreview; } set { showPreview = value; } }
[UrlProperty]
public string PreviewImage { get { return previewImage; } set { previewImage = value; } }
public bool Multi { get { return multi; } set { multi = value; } }
#endregion
#region Page Load
protected void Page_Load(object sender, EventArgs e)
{
ClientScriptManager s = this.Page.ClientScript;
if (!s.IsClientScriptIncludeRegistered(\"LoadUploadify\"))
s.RegisterClientScriptInclude(\"LoadUploadify\", this.Page.ResolveUrl(\"~/Javascript/jquery.uploadify.js\"));
if (!IsPostBack)
{
uploader = Page.ResolveUrl(\"~/JavaScript/uploader.swf\");
buttonImg = Page.ResolveUrl(\"~/images/browse3.png\");
cancelImg = Page.ResolveUrl(\"~/images/cancel.png\");
script = script == \"\" ? Page.ResolveUrl(\"~/Scripts/upload.ashx\") : Page.ResolveUrl(script);
plSizeLimit.Visible = showSizeLimit;
plSizeLimit.CssClass = sizeLimitClass;
imgPreview.ImageUrl = previewImage;
hlPreview.Visible = showPreview;
}
}
#endregion
#region Load Variables
public void Reload()
{
ScriptManager.RegisterStartupScript(this.Page, typeof(Page), \"LoadVars\" + ClientID, \"LoadUploadVariables_\" + ClientID + \"();\", true);
}
public string GenerateScriptData()
{
string data = \"\";
if(scriptData != null)
foreach (KeyValuePair<string, string> kvp in scriptData)
data += \",'\" + kvp.Key + \"' : '\" + kvp.Value + \"'\";
data += \",'ClientID':'\" + this.ClientID + \"'\";
return \"{ \" + data.Substring(1) + \" }\";
}
#endregion
$(window).load(
function() {
<% if(this.Visible) { %>
LoadUploadVariables_<%=ClientID %>();
<% } %>
}
);
function LoadUploadVariables_<%=ClientID %>()
{
$(\"#<%=fileUpload.ClientID %>\").fileUpload({
'uploader': '<%=uploader %>',
'buttonImg': '<%=buttonImg %>',
'cancelImg': '<%=cancelImg %>',
'folder': '<%=Folder %>',
'script': '<%=script %>',
'fileExt': '<%=FileExt %>',
'fileDesc': '<%=FileDesc %>',
'sizeLimit': '<%=SizeLimit %>',
'scriptData': <%=GenerateScriptData() %>,
'auto': <%=Auto.ToString().ToLower() %>,
'width': <%=width %>,
'height': <%=height %>,
'onSelect': <%=OnSelect %>,
'onComplete': <%=OnComplete %>,
'onError': <%=OnUploadError %>,
'multi': <%=Multi.ToString().ToLower() %>
});
}
using System;
using System.Web;
using System.IO;
public class Upload : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = \"text/plain\";
context.Response.Expires = -1;
System.Drawing.Image original_image = null;
try
{
HttpPostedFile jpeg_image_upload = context.Request.Files[\"Filedata\"];
try
{
// Retrieve the uploaded image
original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream);
}
catch (Exception ex)
{
}
string savepath = \"\";
string urlpath = \"~/UploadedFiles/\";
if (context.Request.QueryString[\"folder\"] != null)
{
string tempPath = context.Server.UrlDecode(context.Request.QueryString[\"folder\"]);
urlpath = tempPath;
if (!tempPath.StartsWith(\"~\"))
tempPath = \"~\" + tempPath;
if (!tempPath.EndsWith(\"/\"))
tempPath += \"/\";
savepath = context.Server.MapPath(tempPath);
}
else
savepath = context.Server.MapPath(urlpath);
string filename = jpeg_image_upload.FileName;
string tempName = filename;
int i = 1;
if (!Directory.Exists(savepath))
Directory.CreateDirectory(savepath);
while (File.Exists(savepath + \"/\" + filename))
{
string ext = tempName.Substring(tempName.LastIndexOf(\".\") + 1);
filename = tempName.Substring(0, tempName.LastIndexOf(\".\")) + i + \".\" + ext;
i++;
}
jpeg_image_upload.SaveAs(savepath + @\"\\" + filename);
context.Response.Write(urlpath + \"/\" + filename);
context.Response.StatusCode = 200;
}
catch (Exception ex)
{
context.Response.Write(\"Error: \" + ex.Message);
}
}
public bool IsReusable {
get {
return false;
}
}
}
Lindsay said:I've uploaded a VS2008 C# 3.5 sample for anyone that needs it.
There's also a VS2005 VB.NET 2.0 sample... forgive my VB, it's not my forte. However, for some reason, I decided to add more comments to the VB version.
Let me know if you have any questions!
function LoadUploadVariables_<%=ClientID %>()
{
$("#<%=fileUpload.ClientID %>").fileUpload({
'uploader': '<%=uploaderSwf %>',
'buttonImg': '<%=buttonImg %>',
'cancelImg': '<%=cancelImg %>',
'folder': '<%=Folder %>',
'script': '<%=script %>',
'fileExt': '<%=FileExt %>',
'fileDesc': '<%=FileDesc %>',
'sizeLimit': '<%=SizeLimit %>',
'scriptData': <%=GenerateScriptData() %>,
'auto': <%=Auto.ToString().ToLower() %>,
'width': <%=width %>,
'height': <%=height %>,
'onSelect': <%=OnSelect %>,
'onComplete': <%=OnComplete %>,
'onError': <%=OnUploadError %>,
'multi': true
});
}