文件上传控制文件路径 [英] fileupload control file path

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

问题描述

您好,先生.

我正在使用文件上传控制来上传文件夹中的图像.
图像已正确上传到文件夹,但我想将图像路径保存在数据库中.
如何在数据库中保存文件路径?
有人可以帮我吗?

我的代码如下:

Hello sir.

I am using file upload contol to upload an image in folder.
The image is uploading to folder properly, but I want to save the image path in the database.
How can I save the file path in the database?
Can anybody help me please?

My code is given below:

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.IO;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        try
        {
            String picture;
            if (FileUpload1.PostedFile.FileName.EndsWith(".jpg") || FileUpload1.PostedFile.FileName.EndsWith(".gif") || FileUpload1.PostedFile.FileName.EndsWith(".png"))
            {
                string strfilename;

                //string filename = Path.GetFileName(FileUpload1.FileName);

                string File_Path_Text = FileUpload1.PostedFile.InputStream.ToString();
                //string filepath = FileUpload1.PostedFile.ContentLength();

                strfilename = FileUpload1.PostedFile.FileName;
                strfilename = strfilename.Substring(strfilename.LastIndexOf("\\") + 1);
                FileUpload1.SaveAs(Server .MapPath("\\Test\\" + strfilename));
                Server.MapPath("~/images/" + FileUpload1.FileName);
                picture = strfilename;

                SqlParameter [] oparam=new SqlParameter[3];
                oparam[0]=new SqlParameter("@LocationId", TxtLocationId .Text );
                oparam[1]=new SqlParameter("@LocationName", TxtLocationName.Text );
                oparam[2] = new SqlParameter("@Image", File_Path_Text);

                DataSet ds = new DataSet();
                ds = BusinessLogic.InsertImage(oparam);
                //LblShow.Text = "File Saved at:" + filepath;
            }

            //if (FileUpload1.PostedFile.ContentLength != 0)
            //{

            //    DataSet ds = new DataSet();

            //    FileUpload1.SaveAs(@"Test\\" + FileUpload1.FileName);

            //    Server.MapPath("images\\" + FileUpload1.FileName);

                //string path = FileUpload1.PostedFile.FileName;
                //FileUpload1.SaveAs("@\\Test\\" + FileUpload1.FileName);

                //Server.MapPath("\\images\\" + FileUpload1.FileName);
                //SqlParameter [] oparam=new SqlParameter[4];
                //oparam[0]=new SqlParameter("@LocationId", TxtLocationId .Text );
                //oparam[1]=new SqlParameter("@LocationName", TxtLocationName.Text );
                //oparam[2]=new SqlParameter ("@Image", FUImage .FileName );

                //DataSet ds = new DataSet();
            //}
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

推荐答案

从您发布的代码中,看来BusinessLogic.InsertImage()是完成工作的方法.

除此之外,如果不使用相应的代码就无法为您提供帮助.
From the code you have posted, it would seem that BusinessLogic.InsertImage() is the method that does the work.

Beyond that it is impossible to help you without seing the code for that.


INSERT INTO table(FilePath) VALUES (FileUpload.PostedFile.FileName)



在AFAIU中,您可以在代码中运行此查询,并且您的路径将在数据库中.

还是我想念什么?请输入您的意见以寻求进一步的解决方案.



AFAIU you can run this query inside your code and you will have your path in database.

Or I am missing something ? Kindly give your inputs for further solution.


SqlParameter [] oparam=new SqlParameter[3];
                oparam[0]=new SqlParameter("@LocationId", TxtLocationId .Text );
                oparam[1]=new SqlParameter("@LocationName", TxtLocationName.Text );
//@Image is different from procedure @Imagepath
                oparam[2] = new SqlParameter("@ImagePath", FileUpload1.FileName);
//for complete path use
oparam[2] = new SqlParameter("@ImagePath", FileUpload1.PostedFile.FileName);


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

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