在数据库中插入上载的文件地址(url) [英] Insert Uploaded file address(url) in database

查看:152
本文介绍了在数据库中插入上载的文件地址(url)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生......

如何在数据库中找到并插入上传文件的地址





Sir...
How can i find and insert the address of the uploaded file in database


protected void btnInsert_Click(object sender, EventArgs e)
    {
        cmd.CommandText = "sp_Admininsert";
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("@Title", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Author", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Address", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Url", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Reference", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Abstract", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Categories", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Year", SqlDbType.NVarChar);


        cmd.Parameters["@Title"].Value = txtTitle.Text.Trim();
        cmd.Parameters["@Author"].Value = TextAuthor.Text.Trim();
        cmd.Parameters["@Address"].Value = TextAddress.Text.Trim();
        cmd.Parameters["@Reference"].Value = TextReference.Text.Trim();
        cmd.Parameters["@Abstract"].Value = TextAbstract.Text.Trim();
        cmd.Parameters["@Categories"].Value = TextCategories.Text.Trim();
        cmd.Parameters["@Year"].Value = TextYear.Text.Trim();
        cmd.Parameters["@Url"].Value =



        cmd.Parameters.Add("@retid", SqlDbType.Int);
        cmd.Parameters["@retid"].Direction = ParameterDirection.ReturnValue;

        con.Open();
        cmd.ExecuteNonQuery();
        string id = cmd.Parameters["@retid"].Value.ToString();
        cmd.Parameters.Clear();
        con.Close();

        fupPic.SaveAs(MapPath("~/PDF/" + TextYear.Text.Trim() + "/" + id + Path.GetExtension(fupPic.FileName)));
    }
}

推荐答案



我的建议是不要在db中存储整个url。只需存储上传的文件名。因为下次从db中检索它时。您可以使用 Server.MapPath(〜/ PDF /+ yourfilenamefromdb);



如果您要上传多个类型的文件,然后存储相关的路径。

例如,在您的示例中,将其存储为PDF / filename



谢谢。
Hi,
My advice is don't store whole url in db. Just store uploaded file name. Because next time when you retrieve it from db. you can use Server.MapPath("~/PDF/"+yourfilenamefromdb);

If you are uploading multiple type files, then store it with related path.
e.g., In your example store it with "PDF/filename".

Thanks.


如果您使用不同的文件夹来保存文件,那么您应该在文件名之前附加文件夹名称并将其作为文件URL 存储在DataBase中。



否则只需存储文件名



显示文件时,您可以从数据库( Url 字段)获取文件名,然后你知道文件夹名称,所以你只需要附加文件夹名称。然后使用 Server.MapPath 映射路径。就是这样。
If you are using different folders to save the files, then you should append the Folder Name before the File Name and store that in DataBase as the File Url.

Else just store the File Name.

While showing the file, you can get the File Name from Database (Url field) and then as you know the Folder Name, so you just need to append that with the Folder Name. Then Map the path ussing Server.MapPath. That's it.


这篇关于在数据库中插入上载的文件地址(url)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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