62,243
社区成员




<div>
<asp:FileUpload ID="FileUpload1" runat="server" Width="300px" Height="24px" />
<asp:Button ID="btnAdd" runat="server" Height="23px" Text="上传" Width="84px"
OnClick="BtnUpload_Click" CausesValidation="False" />
<asp:HyperLink ID="hpMessage" runat="server" Target="_blank"></asp:HyperLink>
</div>
<asp:TextBox ID="upFileName" runat="server" Visible="false" ></asp:TextBox>
public partial class Management_upPDF : System.Web.UI.UserControl
{
//上传文件的保存路径
private string fileSavaPath = null;
public string FileSavaPath
{
get { return fileSavaPath; }
set { fileSavaPath = value; }
}
//文件格式的形式中间用‘|’隔开---如:jpg|bmp
private string fileFormat = null;
public string FileFormat
{
get { return fileFormat; }
set { fileFormat = value; }
}
//上传文件的新名称
private string fileNewName = null;
public string FileNewName
{
get { return fileNewName; }
set { fileNewName = value; }
}
#region pdf转swf
/// <summary>
///
/// </summary>
/// <param name="appPath">转换软件路径--@"C:\SWFTools\pdf2swf.exe"</param>
/// <param name="Source">源文件保存路径</param>
/// <param name="Des">转换后的文件文件保存路径</param>
/// <param name="filename">文件名</param>
/// <returns></returns>
private Boolean Doc2Swf(string appPath, string Source, string Des, string filename)
{
Process pc = new Process();
ProcessStartInfo psi = new ProcessStartInfo(appPath, Source + " " + Des);
try
{
pc.StartInfo = psi;
pc.Start();
pc.WaitForExit();
//val = filename.Replace(".pdf", ".swf");
//getVal(val);
}
catch
{
return false;
throw;
}
finally
{
pc.Close();
}
return File.Exists(Des);
}
#endregion
protected void BtnUpload_Click(object sender, EventArgs e)
{
//try
//{
//上传文件的原名称
string FullName = FileUpload1.PostedFile.FileName.ToString();
if (FullName.Length <= 0)
{
hpMessage.Text = "请您选择要上传的文件";
hpMessage.NavigateUrl = "";
}
else
{
//获取上传文件的类型
string FileType = FullName.Substring(FullName.LastIndexOf(".") + 1);
//全部转换为大写形式
FileType = FileType.ToUpper();
//将文件格式要求与上传文件格式对比
//Response.Write("<script type='text/javascript'>alert('" + FileType.IndexOf(fileFormat) + "')</script>");
string[] liststr = null;
if (!StringUtils.StrIsNullOrEmpty(fileFormat))
{
//统一转换为大写形式
fileFormat = fileFormat.ToUpper();
//拆分格式
liststr = fileFormat.Split('|');
}
if (liststr != null && liststr.Length > 0)
{
//格式是否正确
bool isOK = false;
if (liststr.Length > 0)
{
foreach (string item in liststr)
{
if (item == FileType)
{
isOK = true;
continue;
}
}
if (isOK == true)
{
string FileName = null;
FileName = String.Format(DateTime.Now.ToString());
//替换个别字符
FileName = FileName.Replace("-", "");
FileName = FileName.Replace(":", "");
FileName = FileName.Replace("/", "");
FileName = FileName.Replace(" ", "");
//int length = FullName.Length - FullName.LastIndexOf("\\") - 1;
//上传文件的名称
fileNewName = FileName + "." + FileType;
//详细保存路径
//Response.Write("<script type='text/javascript'>alert('" + fileNewName + "')</script>");
string Path = Server.MapPath(fileSavaPath) + "\\" + fileNewName;
//上传文件
FileUpload1.SaveAs(Path);
upFileName.Text = fileNewName;
//Response.Write("<script type='text/javascript'>alert('" + Path + "')</script>");
hpMessage.Text = "恭喜您,上传成功!";
hpMessage.NavigateUrl = fileSavaPath + "/" + fileNewName;
//Response.Write("<script type='text/javascript'>parent.form." + FormElement + ".value='" + IName + "'</script>");
//Response.Write("<script type='text/javascript'>window.parent.document.getElementById(\"" + FormElement + "\").value='" + IName + "'</script>");
}
else
{
hpMessage.Text = "<font color='red'>请确认上传文件的格式是否正确!</font>";
}
}
}
}
//}
//catch (Exception ex)
//{
//}
}