文件上传问题,找不到文件 [英] File Upload problem, file not found

查看:157
本文介绍了文件上传问题,找不到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi,
From my asp.net form, I am trying to save a picture to my SQL Database.  Problem is the File Upload button just takes the name of the file, not the full path.  When I try to save it, it says file not found.
Is there something like OpenFileDialog in webforms, as I use that for vb.net and that works excellent.

Thanks

推荐答案

那是因为FileUpload控件不会将文件保存到磁盘-它不知道您要做什么它.
这是我使用的代码:

That''s because the FileUpload control does not save the file to disk - it doesn''t know what you want to do with it.
Here is the code I use:

/// <summary>
/// Save an upload into the database.
/// </summary>
/// <param name="fl">Control containing the download.</param>
/// <returns>Status as a string</returns>
private string SaveUpload(FileUpload fl)
    {
    if (fl.HasFile)
        {
        try
            {
            int version = 0;
            string filename = Path.GetFileName(fl.FileName);
            byte[] filedata = fl.FileBytes;
            string strCon = ConnectionStrings.Download;
            using (SqlConnection con = new SqlConnection(strCon))
                {
                con.Open();
                // Stick file into database.
                using (SqlCommand ins = new SqlCommand("INSERT INTO dlContent (iD, fileName, description, dataContent, version, uploadedOn) " +
                                                       "VALUES (@ID, @FN, @DS, @DT, @VS, @UD)", con))
                    {
                    ins.Parameters.AddWithValue("@ID", Guid.NewGuid());
                    ins.Parameters.AddWithValue("@FN", filename);
                    ins.Parameters.AddWithValue("@DS", "");
                    ins.Parameters.AddWithValue("@DT", filedata);
                    ins.Parameters.AddWithValue("@VS", version);
                    ins.Parameters.AddWithValue("@UD", DateTime.Now);
                    ins.ExecuteNonQuery();
                    }
                }
            return string.Format("{0} uploaded, version = {1}", filename, version);
            }
        catch (Exception ex)
            {
            return "The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    return "Please select a file.";
    }


这是通过上载按钮单击"事件处理程序调用的.


That gets called from the Upload Button Click event handler.


这是为什么我将图像存储在文件夹中的代码. 并且我还建议您使用文件夹选项,因为我将图像存储在数据库中.然后将其设为tak spac


静态字符串ext;
公共无效btnUpload_Click(对象发送者,System.EventArgs e)
{
HttpPostedFile myFile = filUpload.PostedFile;
if(myFile!=空&& myFile.ContentLength!= 0&& myFile.FileName!=")
{
如果(System.IO.Path.GetExtension(myFile.FileName).ToLower()!=".jpg"&& System.IO.Path.GetExtension(myFile.FileName).ToLower()!=".yuv" && System.IO.Path.GetExtension(myFile.FileName).ToLower()!=".tif&&&&&System.IO.Path.GetExtension(myFile.FileName).ToLower()!=". thm&& System.IO.Path.GetExtension(myFile.FileName).ToLower()!=" .pspimage&& System.IO.Path.GetExtension(myFile.FileName).ToLower()!= ".psd"&& System.IO.Path.GetExtension(myFile.FileName).ToLower()!=".png&& System.IO.Path.GetExtension(myFile.FileName).ToLower() !=" .gif&& System.IO.Path.GetExtension(myFile.FileName).ToLower()!="".bmp")
{
Response.Write(< Script> alert(''请选择仅图像文件'')</Script>");
imgPicture.ImageUrl =";
}
其他
{
if(TxtRef.Value!=" || TxtRef.Value.Length!= 0)
{
ext = System.IO.Path.GetExtension(this.filUpload.PostedFile.FileName);
字符串ID = TxtRef.Value + ext;
myFile.SaveAs(Server.MapPath(@〜/images/" + Id));
imgPicture.ImageUrl =〜/images/" + ID;

conn.Open();
SqlCommand updateimage =新的SqlCommand("update ack set picsize =""+ ext +"其中RefNo =""+ TxtRef.Value +",conn);
updateimage.ExecuteNonQuery();
conn.Close();
}
其他
{
Response.Write(< Script> alert(``请先选择您的参考编号N0 .....'')</Script>");
imgPicture.ImageUrl =";
}
}
}
}


如果您满意,我也会向您发送该示例
hy this is acode where i stored image in folder..
and i also recmmd u to use folder option becoz i u store image in database ..then it tak spac


static string ext;
public void btnUpload_Click(object sender, System.EventArgs e)
{
HttpPostedFile myFile = filUpload.PostedFile;
if (myFile != null && myFile.ContentLength != 0 && myFile.FileName!= "")
{
if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".yuv" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".tif" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".thm" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".pspimage" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".psd" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".png" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".gif" && System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".bmp")
{
Response.Write("<Script>alert(''Please Select Only Image File'')</Script>");
imgPicture.ImageUrl = "";
}
else
{
if (TxtRef.Value != "" || TxtRef.Value.Length != 0)
{
ext = System.IO.Path.GetExtension(this.filUpload.PostedFile.FileName);
string Id = TxtRef.Value+ext;
myFile.SaveAs(Server.MapPath(@"~/images/" +Id));
imgPicture.ImageUrl = "~/images/" + Id;

conn.Open();
SqlCommand updateimage = new SqlCommand("update ack set picsize=''"+ext+"'' where RefNo=''"+TxtRef.Value+"''",conn);
updateimage.ExecuteNonQuery();
conn.Close();
}
else
{
Response.Write("<Script>alert(''Please Select Your Ref N0. firstly....'')</Script>");
imgPicture.ImageUrl = "";
}
}
}
}


and if u r nt satisfy then i will send u that exmple also


这篇关于文件上传问题,找不到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆