读书人

.net上传图片文件解决方法

发布时间: 2012-04-15 18:39:21 作者: rapoo

.net上传图片文件
因为页面是用ajax制成的弹出窗口.
所以不能用 <asp:fileupload runat="server">形式来上传文件,。
(是之前别人做好的代码,测试过这形式不可用,..)
所以只能用
<input type="file" id="fileurl" runat="server" />
<input type="submit" value="确定" action="abc.aspx" class="validator" /> //abc.aspx为本页面
---
string fileurlStr = Request["fileurl"].ToString();
得到 fileurlStr值为文件名,但是没有路径,.
例:完整路径为:D:\abc\a.jpg 那么fileurlStr就等于 a.jpg
----
fileurlStr = fileurl.Value;
这样,fileurlStr 值为空。
---


[解决办法]

C# code
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Text;using System.IO;using Asiastar.NRModel;using Asiastar.NRBLL;namespace Asiastar.NR.FileOperation{    public partial class UpLoad : System.Web.UI.Page    {        public string method = "";        public string FolderId = "";        //public string FileTypeImg = "";        protected void Page_Load(object sender, EventArgs e)        {            FolderId = Request["id"].ToString();            /*判断用户是否登录*/            if (Session["username"] == null)            {                Response.Redirect("Default.aspx");            }            /*判断form表单中的按钮是否被点击*/            method = Request.Form["method"];            if (!string.IsNullOrEmpty(method) && method.Equals("Upload"))            {                FN_UpFiles();//调用函数            }        }        #region    上传文件到数据库和服务器        public void FN_UpFiles()        {            //遍历File表单元素            HttpFileCollection files = HttpContext.Current.Request.Files;            try            {                for (int iFile = 0; iFile < files.Count; iFile++)                {                    //检查文件扩展名字                    HttpPostedFile postedFile = files[iFile];                    string fileName = "";//定义文件名                    //string fileExtension = "";                    fileName = Path.GetFileName(postedFile.FileName);//得到上传文件的完整名称 即文件名+后缀名                    int index = fileName.IndexOf(".");                    string FileType = fileName.Substring(index).ToLower();//截取文件后缀名                    //FileTypeImg = "../FileTypeimg/" + hz + ".gif";                    Guid fileGuid = Guid.NewGuid();//生成新的文件名称 以GUID命名防止文件名相同                    string NewFileName = fileGuid.ToString();//新的文件名                    NewFileName = NewFileName + FileType;//新的文件名+后缀名                    if (fileName != "")//如果文件名不为空                    {                        try                        {                            //文件虚拟路径                            string strpath = System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName;                            try                            {                                NRModel.File model = new NRModel.File();                                NRBLL.File bf = new NRBLL.File();                                Guid guid1 = Guid.NewGuid();                                Guid guid2 = new Guid(FolderId);                                Guid guid3 = Guid.NewGuid();                                Guid guid4 = Guid.NewGuid();                                model.Fileid = guid1;                                model.Folderid = guid2;                                model.Filepath = strpath;                                model.FileNam = fileName;                                model.FileSize = postedFile.ContentLength;                                model.Decription = this.TextArea1.Value.ToString();                                model.CreateOn = DateTime.Now;                                model.CreateBy = guid3;                                model.ModefyBy = guid4;                                if (bf.FN_AddNewRes(model) > 0)                                {                                    NR.Error.Log.LogType("上传资源" + fileName + "成功!" + "服务器路径:" + strpath);                                    //保存文件到指定目录(虚拟目录)                                    postedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName);                                    //Page.RegisterStartupScript("提示", "<script language='javascript'>alert('上传成功!');self.opener.location.reload();window.close();</script>");                                    AlertMsg("上传成功!");                                }                            }                            catch (Exception ex)                            {                                NR.Error.Log.LogType(ex.ToString());                            }                        }                        catch (Exception ex)                        {                            NR.Error.Log.LogType(ex.ToString());                        }                    }                    else                    {                        Response.Write("上传文件不能为空!");                        NR.Error.Log.LogType("文件不能为空!");                    }                }            }            catch (System.Exception ex)            {                NR.Error.Log.LogType(ex.ToString());            }        }        #endregion        /// <summary>        /// 弹出对话框并且重定向至页面        /// </summary>        /// <param name="msg">消息内容</param>        /// <param name="url">页面地址</param>        public static void AlertMsg(string msg)        {            HttpContext.Current.Response.Clear();            HttpContext.Current.Response.Write(string.Concat("<script>alert('", msg, "');self.opener.location.reload();window.close();</script>"));            HttpContext.Current.Response.Flush();            HttpContext.Current.ApplicationInstance.CompleteRequest();            HttpContext.Current.Response.End();        }    }} 


[解决办法]
http://www.cnblogs.com/cloudgamer/archive/2009/12/22/ImagePreview.html
[解决办法]
去看看html怎么上传的图片你就弄明白了
[解决办法]
比如你上传的文建目录为d:\abc.jgp
//接收文件
HttpFileCollection Files = Request.Files;
HttpPostedFile File = Files["fileurl"];
//保存文件
File.SaveAs(Server.MapPath("c:\def.jpg"));

//注意:有的电脑上File.FileName取到的是文件名abc.jpg,但是有取到的是全路径d:\abc.jpg。但是无论取到的是什么,都不会影响到文件的上传File.DaveAs(文件保存路径);


[解决办法]
需要给<input name='inpUp' type='file' />指定name属性
[解决办法]

探讨

测试过过1楼的代码,
因为我是弹出页面,所以页面上没有<form>
所以 method = Request.Form["method"]; 这里,method是null值
files.Count的值也为0;
所以,运行不起来,。

[解决办法]
探讨

不能加form,加了form弹出页面就出现错误了,

[解决办法]
http://blog.csdn.net/fengyarongaa/article/details/6640211

看我这个吧 多文件上传

[解决办法]
用ajax制成的弹出窗口.
也可以用 <asp:fileupload runat="server">的
只是要根据你的要求,设置form的位置即可
[解决办法]
为什么不能加FORM,这个FORM不一定非要runat=server啊。
然后action到一个handler或者ashx。
[解决办法]
探讨

为什么不能加FORM,这个FORM不一定非要runat=server啊。
然后action到一个handler或者ashx。

读书人网 >asp.net

热点推荐