quarta-feira, 11 de abril de 2012

Upload simples (ASP.NET)

Segue abaixo o código a ser colocado no aspx:

Single File Upload:



Multi-File Upload:



">


A seguir o código a ser colocado no cs (code behind):

using System;using System.Collections;using System.Collections.Specialized;
using System.Configuration;using System.Text;
using System.Text.RegularExpressions;using System.Web;
using System.Web.Caching;using System.Web.SessionState;
using System.Web.Security;using System.Web.Profile;
using System.Web.UI;using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
partial class _Default : System.Web.UI.Page{ override protected void OnInit(EventArgs e)
{
base.OnInit(e);buttonUpload.Click += buttonUpload_Click;buttonMultiUpload.Click += buttonMultiUpload_Click;
}
override protected void OnLoad(System.EventArgs e){ base.OnLoad(e);if (! IsPostBack)
{
UploadedFiles.DataBind();
}
}
protected string[] GetUploadList(){ string folder = Server.MapPath("~/Uploads");string[] files = Directory.GetFiles(folder);
string[] fileNames = new string[files.Length];Array.Sort(files);
for (int i = 0; i < files.Length; i++)
{
fileNames[i] = Path.GetFileName(files[i]);
}
return fileNames;
}
protected void UploadThisFile(FileUpload upload)
{
if (upload.HasFile)
{
string theFileName = Path.Combine(Server.MapPath("~/Uploads"), upload.FileName);
if (File.Exists(theFileName))
{
File.Delete(theFileName);
}
upload.SaveAs(theFileName);
}
}
protected void buttonUpload_Click(object sender, System.EventArgs e)
{
UploadThisFile(FileUpload1);
UploadedFiles.DataBind();
}
protected void buttonMultiUpload_Click(object sender, System.EventArgs e)
{
UploadThisFile(multiUpload1);
UploadThisFile(multiUpload2);
UploadThisFile(multiUpload3);
UploadedFiles.DataBind();
}
}

Um comentário:

  1. Boa tarde Jorge, tem algum meio de eu entrar em contato com você ?

    Preciso tirar umas duvidas sobre esse post seu:

    http://jorgejcosta.blogspot.com.br/2011/08/criando-appointment-via-codigo-c.html

    Grato, Sérgio.
    serginho_vs@hotmail.com
    twitter.com/sehmv

    ResponderExcluir