表单是:
<form id="form1" runat="server" enctype="multipart/form-data">
<INPUT contentEditable="false" type="file" size="40" name="m_file" id="m_file">
<br />
<asp:Button id="Button1" runat="server" Text="上传" onclick="Button1_Click"></asp:Button>
</form>.cs中我想取到这个文件的名称,但得到的为空UploadFile my_file = m_upload.GetUploadFile("m_file");下面是用到的相关函数 public static HttpContext GetContext()
{
HttpContext context = HttpContext.Current;
if (context == null)
{
throw new Exception("HttpContext not found");
}
return context;
} public UploadFile GetUploadFile(string name)
{
            string content = WebbHelper.GetContext().Request.Form[name];//这里,我得到的值怎么都是空,应该怎么改呢?
            
            if ((content == null) || (content == string.Empty)) return null;
            if (!IsEmptyFileContent(content)) return null;
UploadFile uploadFile = new UploadFile(name);
return (uploadFile.FileName == string.Empty) ? null : uploadFile;
}
求助,如何才能获取到其值,并能保证可以正常上传呢,用的是修改过的LargeFileUpload类,主要是上传出现问题。

解决方案 »

  1.   

    asp.net2.0中有专门上传文件的控件,用这个专门上传文件的控件好了。
    如果你要使用<INPUT contentEditable="false" type="file" size="40" name="m_file" id="m_file">
    ,好像要在form中添加一个东西
      

  2.   


    在Form节里增加enctype="multipart/form-data定义
      

  3.   

    <INPUT contentEditable="false" type="file" size="40" name="m_file" id="m_file">
    将他设为服务器控件<INPUT contentEditable="false" type="file" size="40" name="m_file" id="m_file" runat="server">后台直接用ID.的形式不就可以用了。写那么多代码干嘛?
      

  4.   

    在form里面加上这句话enctype="multipart/form-data
      

  5.   

    我觉得还是用ASP.NET封装好的上传控件比较好。
      

  6.   

            FileUpload fu = (sender as Button).Parent.FindControl("FileUpload1") as FileUpload;
            string _Extend = System.IO.Path.GetExtension(fu.FileName);
            _Extend = _Extend.ToLower();
            if (_Extend == ".doc" || _Extend == ".xls" || _Extend == ".txt" || _Extend == ".jpg" || _Extend == ".gif" || _Extend == ".rar")
            {
                if (fu.HasFile)
                {
                    string path = Server.MapPath(this.AppRelativeVirtualPath);
                    path = string.Format("{0}\\Files", System.IO.Path.GetDirectoryName(path));
                    if (!System.IO.Directory.Exists(path))
                    {
                        System.IO.Directory.CreateDirectory(path);
                    }                string filename = System.IO.Path.GetFileName(fu.FileName);
                    fu.SaveAs(string.Format("{0}\\{1}", path, filename));
                    HiddenField field = (sender as Button).Parent.FindControl("HiddenField1") as HiddenField;
                    field.Value = filename;
                    this.ClientScript.RegisterStartupScript(typeof(string), string.Empty, "<script>alert('文件上传成功!')</script>");
                }
            }
            else
            {
                ut.Alert("请检查上传的文件格式!\\n支持上传的文件格式有:txt,jpg,gif,xls,doc,rar", this);
                return;
            }
      

  7.   

    string content = WebbHelper.GetContext().Request.Form[name];//这里,我得到的值怎么都是空,应该怎么改呢?这一行完全多余。
    如果你是为了要判断用户是否选择了文件,可以使用下面的代码bool fileUploaded = string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.Files[0].FileName;
      

  8.   

    补充:我现在是想知道如何去修正取值为空的问题关于上传的结构如下:
    public class UploadFile
    {
    #region Properties /// <summary>
    /// Gets the length (in bytes) of the uploaded file.
    /// </summary>
    public long FileSize
    {
    get { return this.m_filelength; }
    } /// <summary>
    /// Gets the MIME content type of the uploaded file.
    /// </summary>
    public string ContentType
    {
    get { return this.m_contenttype; }
    } /// <summary>
    /// Gets the file name(including path) of the uploaded file as it was on the client machine.
    /// </summary>
    public string FileName
    {
    get { return this.m_filename; }
    } /// <summary>
    /// Gets the file name and path on client.
    /// </summary>
    public string ClientFullPathName
    {
    get{return this.m_clientName;}
    }
    /// <summary>
    /// Gets the file extenion name.
    /// </summary>
    public string ExtendName
    {
    get
    {
                    return this.m_filename.Substring(this.m_filename.LastIndexOf("."));
    }
    }
    #endregion #region Fields
    private string m_contenttype;
    private long m_filelength;
    private string m_filename;
    private string m_clientName;
    private string m_filePath;
    #endregion
    /// <summary>
    /// 
    /// </summary>
    /// <param name="name"></param>
    public UploadFile(string i_name)
    {
    if(i_name == null||i_name == string.Empty)
    {
    return;
    // throw new ArgumentNullException("i_name", "Name can not be null!");
    } string m_content = string.Empty;
    this.m_clientName = string.Empty;
    this.m_filename = string.Empty;
    this.m_contenttype = string.Empty;
    this.m_filelength = 0;

    if(IsContentHeader(i_name))
    {
    m_content = i_name;
    }
    else if(IsContentHeader(WebbHelper.GetContext().Request[i_name]))
    {
    m_content = WebbHelper.GetContext().Request[i_name];
    }
    if((m_content==null)||(m_content==string.Empty))
    {
    return;
    }
    //Get file info from content.
    string[] contentArray = m_content.Split(';');
    string m_temp = contentArray[0];
    this.m_contenttype = m_temp.Substring(m_temp.IndexOf(":")+1).Trim();
    m_temp = contentArray[1];
    this.m_clientName = m_temp.Substring(m_temp.IndexOf("\"")+1,m_temp.LastIndexOf("\"")-m_temp.IndexOf("\"")-1).Trim();
    m_temp = contentArray[2];
    this.m_filename = m_temp.Substring(m_temp.IndexOf("\"")+1,m_temp.LastIndexOf("\"")-m_temp.IndexOf("\"")-1).Trim();
    string uploadFolder = WebbHelper.GetUploadFolder();
    //string uploadFolder = @"C:\Inetpub\wwwroot\WebbTest\Upload";
    if(this.m_filename==null||this.m_filename==string.Empty) return;
    this.m_filePath = Path.Combine(uploadFolder, this.m_filename);
    try
    {
    this.m_filelength = new FileInfo(this.m_filePath).Length;
    }
    catch (Exception exception)
    {
    string uploadGuid = WebbHelper.GetContext().Request["Webb_Upload_GUID"];
    if (uploadGuid != null)
    {
    WebbHelper.GetContext().Application.Remove(("Upload_Status_" + uploadGuid));
    }
    throw exception;
    }
    } /// <summary>
    /// 
    /// </summary>
    /// <param name="line"></param>
    /// <returns></returns>
    private bool IsContentHeader(string i_line)
    {
    //WebbSystem.TraceMsg(line);
    if((i_line == null)||(i_line == String.Empty))
    {
    return false;
    }
    string[] contentArray = i_line.Split(';'); if (contentArray.Length==3
    && contentArray[0].IndexOf("Content-Type:")>=0
    && contentArray[1].IndexOf("filename=\"")>=0
    && contentArray[2].IndexOf("filePath=\"")>=0)
    {
    return true;
    }
    return false;
    } /// <summary>
    /// Save file to disk.
    /// </summary>
    /// <param name="filename"></param>
    public void SaveAs(string filename)
    {
    string uploadGuid = WebbHelper.GetContext().Request["Webb_Upload_GUID"];
    string m_fileName = Path.GetFileName(filename);
    if(m_fileName==null||m_fileName==string.Empty) return;
    if(this.m_filePath==null||this.m_filePath==string.Empty) return;
    try
    {
    UploadStatus uploadStatus;
    FileInfo fileInfo = new FileInfo(this.m_filePath);
    if (uploadGuid != null)
    {
    uploadStatus = new UploadStatus();
    uploadStatus.GetUploadStatus(uploadGuid);
    uploadStatus.Status = UploadStatus.UploadState.Moving;
    WebbHelper.GetContext().Application[("Upload_Status_" + uploadGuid)] = uploadStatus;
    } string directoryName = Path.GetDirectoryName(filename);
    if (!Directory.Exists(directoryName))
    {
    Directory.CreateDirectory(directoryName);
    }
    else if (File.Exists(filename))
    {
    File.Delete(filename);
    }
    //Move temporary file to file that client uploaded. Simply change it's name.
    fileInfo.MoveTo(filename);
    if (uploadGuid == null)
    {
    return;
    }
    uploadStatus = new UploadStatus();
    uploadStatus.GetUploadStatus(uploadGuid);
    uploadStatus.Status = UploadStatus.UploadState.Completed;
    WebbHelper.GetContext().Application[("Upload_Status_"+uploadGuid)]=uploadStatus;
    }
    catch (Exception exception)
    {
    if (uploadGuid != null)
    {
    WebbHelper.GetContext().Application.Remove(("Upload_Status_" + uploadGuid));
    }
    throw exception;
    }
    }
    }怎么取值,才能从表单中得到这样的结构呢直接用UploadFile my_file = m_upload.GetUploadFile("m_file"); //m_file是表单中的一个input
    得到的为什么总是空呢。
      

  9.   

    --------------------------
    internal class UploadStatus:IDisposable
    {
    public enum UploadState : byte
    {
    Initializing = 0,
    Uploading = 1,
    Uploaded = 2,
    Moving = 3,
    Completed = 4,
    Error = 5
    } #region Fields
    private long m_dataLength;
    private long m_readLength;
    private DateTime m_beginTime;
    private int m_fileCount;
    private string m_fileName; //File name on client PC when uploading.
    private bool m_isActive;
    private string m_uploadID;
    private UploadState m_status;
    #endregion
            
    #region properties
    public TimeSpan LeftTime
    {
    get
    {
    if(this.Speed==0)
    {
    return System.DateTime.Now.Subtract(System.DateTime.Now);
    }
    else
    {
    long m_temp = (this.m_dataLength-this.m_readLength)/this.Speed;
    return System.DateTime.Now.Subtract(System.DateTime.Now.AddSeconds(-m_temp));
    }
    }
    }
    public bool IsActive
    {
    get{return m_isActive;}
    set{this.m_isActive=value;}
    }
    public string FileName
    {
    get{return m_fileName;}
    set{this.m_fileName = value;}
    }
    public long Speed
    {
    get
    {
    TimeSpan m_timeSpan = DateTime.Now.Subtract(this.m_beginTime);
    long m_spendSec = Convert.ToInt64(m_timeSpan.TotalSeconds);
    if(m_spendSec<=0) m_spendSec = 1;
    return this.m_readLength/m_spendSec;
    }
    }
    public int FileCount
    {
    get{return this.m_fileCount;}
    set{this.m_fileCount= value;}
    }
    public string UploadGUID
    {
    get{return this.m_uploadID;}
    set{this.m_uploadID=value;}
    }
    public UploadState Status
    {
    get{return m_status;}
    set{this.m_status = value;}
    }
    public long Percent
    {
    get
    {
    if(m_dataLength!=0)
    {
    return (100*m_readLength/m_dataLength);
    }
    return 0;
    }
    }
    public long AllDataLength
    {
    get{return this.m_dataLength;}
    set
    {
    if(value<0)
    {
    this.m_dataLength = 1;
    }
    else
    {
    this.m_dataLength = value;
    }
    }
    }
    public DateTime BeginTime
    {
    get{return this.m_beginTime;}
    set{this.m_beginTime = value;}
    }
    public long ReadData
    {
    get{return this.m_readLength;}
    set{this.m_readLength = value;}
    }
    #endregion

    public UploadStatus()
    {
    this.m_beginTime = System.DateTime.Now;
    this.m_fileCount = 0; //1
    this.m_fileName = string.Empty;
    this.m_dataLength = 100L; //100
    this.m_readLength = 0L; //1
    this.m_uploadID = Guid.NewGuid().ToString();
    this.m_status = UploadState.Initializing;
    this.m_isActive = true;
    } public void ResetBeginTime()
    {
    this.m_beginTime = System.DateTime.Now;
    } public void GetUploadStatus(string m_uploadGUID)
    {
    UploadStatus m_status = HttpContext.Current.Application[("Upload_Status_"+m_uploadGUID)] as UploadStatus;
    if(m_status!=null)
    {
    this.m_beginTime = m_status.BeginTime;
    this.m_fileCount = m_status.FileCount;
    this.m_fileName = m_status.FileName;
    this.m_dataLength = m_status.AllDataLength;
    this.m_readLength = m_status.ReadData;
    this.m_uploadID = m_status.UploadGUID;
    this.m_status = m_status.Status;
    this.m_isActive = true;
    }
    else
    {
    this.m_isActive = false;
    }
    }
    #region un-initializtion and dispose
    public void Dispose()
    {
    } ~UploadStatus()
    {
    }
    #endregion
    }这个有里面具体的结构吧。
      

  10.   

    回楼上的,用的就是文件上传控件CSDN上有提供的LargeFileUpload这个控件。
      

  11.   

    继续求助关于大文件上传类,现在查到原因,主要就是这个大文件上传类不知道怎么调用CSDN上下载了这个源代码,但一调试就显示什么没有集成windows认证,但在web.config中又是加入了windows这个认证的。真是奇怪。继续求达人能帮忙解决一下关于大文件传类的源码和示例,我下载的是这个:
    http://download.csdn.net/source/236462