怎么在上传文件是变成动态添加,可以传一个或多个图片

解决方案 »

  1.   


            If (FileUpload1.HasFile) Then
                Dim filename As String = Replace(FileUpload1.FileName, " ", "")
                savepath += filename
                FileUpload1.SaveAs(savepath)
            End If
      

  2.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;public partial class upload : System.Web.UI.Page
    {
        string picPath = "";
        string picServer = "/upload";
        protected string itemID = "";
        protected void Page_Load(object sender, EventArgs e)
        {        if (Request.QueryString["id"] != null)
            {
                itemID = Request.QueryString["id"];
            }
      
            if (IsPostBack)
            {
                picPath = Server.MapPath("\\upload");
                doUpload();
            }
        }    protected void doUpload()
        {
            try
            {
                HttpPostedFile file = file1.PostedFile;
                string strNewPath = GetSaveFilePath() + GetExtension(file.FileName);
                file.SaveAs(picPath+strNewPath);
                string urlPath = picServer + strNewPath;
                urlPath = urlPath.Replace("\\", "/");
                WriteJs("parent.uploadsuccess('" + urlPath + "','" + itemID + "'); ");
                
            }
            catch (Exception ex)
            {
                WriteJs("parent.uploaderror();");            
            }
        }    private string GetExtension(string fileName)
        {
            try
            {
                int startPos = fileName.LastIndexOf(".");
                string ext = fileName.Substring(startPos, fileName.Length - startPos);
                return ext;
            }
            catch (Exception ex)
            {
                WriteJs("parent.uploaderror('" + itemID + "');");
                return string.Empty;
            }
        }    private string GetSaveFilePath()
        {
            try
            {
                DateTime dateTime = DateTime.Now;
                string yearStr = dateTime.Year.ToString(); ;
                string monthStr = dateTime.Month.ToString();
                string dayStr = dateTime.Day.ToString();
                string hourStr = dateTime.Hour.ToString();
                string minuteStr = dateTime.Minute.ToString();
                string dir = dateTime.ToString(@"\\yyyyMMdd");
                if (!Directory.Exists(picPath + dir))
                {
                    Directory.CreateDirectory(picPath + dir);
                }
                return dir + dateTime.ToString("\\\\yyyyMMddhhmmssffff");
            }
            catch (Exception ex)
            {
                WriteJs("parent.uploaderror();");
                return string.Empty;
            }
        }    protected void WriteJs(string jsContent)
        {        
            this.Page.RegisterStartupScript("writejs","<script type='text/javascript'>"+ jsContent+"</script>");
        }}