文件保存问题 [英] File saving problem
本文介绍了文件保存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我该如何保存文件在数据库中的地址和文件在文件夹中,并且仅在数据库中显示文件名.
How could i save address of file in a database and file in a folder and only name of file is shown in database,.
推荐答案
我将保存在ArrayList位于一个文件夹的文件名中.您可以将ArrayList保存在数据库中.您可以检查以下代码.
I will save in ArrayList in the file name of one folder.You can save the ArrayList in your database.You can check the following code.
//FIND ALL FILES IN FOLDER
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Server.MapPath("~/FolderName"));
ArrayList arrFilesName = new ArrayList();
foreach (System.IO.FileInfo f in dir.GetFiles("*.*"))
{
//LOAD FILES
arrFilesName.Add(f.Name);
}
希望对您有所帮助,
Theingi Win
Hope be helpful,
Theingi Win
if (fileUpload1.HasFile)
{
string FolderName = "Gallery\\";
DirectoryInfo thisFolder = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\Resources\\" + FolderName);
if (!thisFolder.Exists)
{
thisFolder.Create();
}
strunFilepath = "~/Resources/" + FolderName + "/" + FileName;
filename = "~/Resources/" + FolderName + FileName;
}
oHttpPostedFile.SaveAs(AppDomain.CurrentDomain.BaseDirectory + strunFilepath.Replace("~", ""));
要保存在数据库中,请保存文件名".
For saving in the database save ''filename'' .
它可以很好地工作,只需将其复制并在遇到任何问题时使用,如果您回复我,我会告诉您...
在下面的代码"fuphotos"中是我的文件上传控件的ID
it works fine for just copy this and use if you face any problem i will tell you if you reply me...
in below code "fuphotos" is the id of my fileupload control
protected void imgbtnSave_Click(object sender, ImageClickEventArgs e)
{
HttpPostedFile oHttpPostedFile = fuphotos.PostedFile;
string uncFileName = string.Empty;
string ImageName = string.Empty;
string StrFileType = string.Empty;
string strunFilepath = string.Empty;
string strcFilepath = string.Empty;
string strcFilepath1 = string.Empty;
try
{
if (fuphotos.HasFile)
{
Normal image saving starts
StrFileType = oHttpPostedFile.ContentType.ToString();
if (StrFileType == "image/pjpeg" || StrFileType == "image/gif" || StrFileType == "image/jpeg" || StrFileType == "image/bmp")
{
uncFileName = oHttpPostedFile.FileName.Substring(oHttpPostedFile.FileName.LastIndexOf("\\") + 1);
string FolderName = "UNC_PhotoGallery\\";
DirectoryInfo thisFolder = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\Resources\\" + FolderName);
if (!thisFolder.Exists)
{
thisFolder.Create();
}
strunFilepath = "~/Resources/" + FolderName + "/" + uncFileName;
}
oHttpPostedFile.SaveAs(AppDomain.CurrentDomain.BaseDirectory + strunFilepath.Replace("~", ""));
}
string insquery = "INSERT INTO PHOTOS_TABLE(PhotoTitle,PhotoPath,PostedDate) VALUES (''" + txtPhotoTitle.Text + "'',''" + strcFilepath + "'',''" + DateTime.Now.ToLongDateString() + "'')";
string result = belObj.Recordinsert(insquery);
if (result == "Success")
Page.ClientScript.RegisterStartupScript(this.GetType(), "aaa", "<script language=''javascript''>fnalert(''Photo Uploaded successfully.'')</script>");
}
catch (Exception ex)
{
//exception
}
}
这篇关于文件保存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文