2014年7月13日 星期日

[RESOLVED] Could not find a part of the path


Hi,


I am trying to upload file either in upload 1 or upload 2 or both and want it to be entered in database by clicking single submit button. But It's giving me error Could not find a part of the path. I can assure you path is right because
when I single upload file and click on submit button it's uploaded but when I try to use two upload box it's giving me error.


It's pointing me to : FileUpload1.SaveAs(Server.MapPath("images1/" + FileName1));



Here is the behind 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.Data.SqlClient;
using System.IO;


public partial class asp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();

String strConnString = System.Configuration.ConfigurationManager.

ConnectionStrings["userConnectionString"].ConnectionString;

string strQuery = "select * from asp order by ID";

SqlCommand cmd = new SqlCommand(strQuery);

SqlConnection con = new SqlConnection(strConnString);

SqlDataAdapter sda = new SqlDataAdapter();

cmd.CommandType = CommandType.Text;

cmd.Connection = con;

try
{

con.Open();

sda.SelectCommand = cmd;

sda.Fill(dt);

GridView1.DataSource = dt;

GridView1.DataBind();

}

catch (Exception ex)
{

Response.Write(ex.Message);

}

finally
{

con.Close();

sda.Dispose();

con.Dispose();

}
}

protected void Upload_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile != null || FileUpload2.PostedFile != null)
{

string FileName1 = Path.GetFileName(FileUpload1.PostedFile.FileName);

string FileName2 = Path.GetFileName(FileUpload1.PostedFile.FileName);

//Save files to disk

FileUpload1.SaveAs(Server.MapPath("images1/" + FileName1));

FileUpload1.SaveAs(Server.MapPath("images1/" + FileName2));

//Add Entry to DataBase

String strConnString = System.Configuration.ConfigurationManager

.ConnectionStrings["userConnectionString"].ConnectionString;

SqlConnection con = new SqlConnection(strConnString);

string strQuery = "insert into asp (FileName1, FilePath1, FileName2, FilePath2)" +

" values(@FileName1, @FilePath1, @FileName2, @FilePath2)";

SqlCommand cmd = new SqlCommand(strQuery);

cmd.Parameters.AddWithValue("@FileName1", FileName1);

cmd.Parameters.AddWithValue("@FilePath1", "images1/" + FileName1);

cmd.Parameters.AddWithValue("@FileName2", FileName2);

cmd.Parameters.AddWithValue("@FilePath2", "images1/" + FileName2);

cmd.CommandType = CommandType.Text;

cmd.Connection = con;




try
{

con.Open();

cmd.ExecuteNonQuery();

}

catch (Exception ex)
{

Response.Write(ex.Message);

}

finally
{

con.Close();

con.Dispose();

}

}



}

Kindly help thanks.




Use the full path with ~ to represent the root, also you're mapping the path before the file has been saved so of course it won't be found.  You need to map the path and then add the desired filename after


FileUpload1.SaveAs(Server.MapPath("~/images1/") + FileName1);

might need


FileUpload1.SaveAs(Server.MapPath("~/images1")
+ "/"
 +
FileName1);


check which one works





AidyF



Use the full path with ~ to represent the root, also you're mapping the path before the file has been saved so of course it won't be found.  You need to map the path and then add the desired filename after
FileUpload1.SaveAs(Server.MapPath("~/images1/") + FileName1);

might need


FileUpload1.SaveAs(Server.MapPath("~/images1") + "/" + FileName1);


check which one works



No same thing.


Problem is when I upload anything on upload box 1, it's automatically uploaded for both table FileName1 and FileName2.


When I try to upload only on upload box 2 it's point me to the error to


FileUpload1.SaveAs(Server.MapPath("~/images1/") + FileName1);

So, I think it's something to do with Upload box 1 with the permission to not allowing to be null or something.





I think you meant to reference FileUpload2 in part. You only are referencing the first upload, not the second. Try changing to:


            string FileName1 = Path.GetFileName(FileUpload1.PostedFile.FileName);

string FileName2 = Path.GetFileName(FileUpload2.PostedFile.FileName);

//Save files to disk

FileUpload1.SaveAs(Path.Combine(Server.MapPath("~/images1/"),FileName1));

FileUpload2.SaveAs(Path.Combine(Server.MapPath("~/images1/"),FileName2));

I like to use Path.Combine when joining files and file paths so that way I never worry about getting it right (Path is in the System.IO namespace)



FileUpload1.SaveAs

(Path.Combine(Server.MapPath

("~/images1/"), FileName1)); or

FileUpload1.SaveAs

(Server.MapPath("~/images1/") +

"\\" + FileName1); move filename out

side server.mappath



new2world



string FileName1 = Path.GetFileName(FileUpload1.PostedFile.FileName);

string FileName2 = Path.GetFileName(FileUpload1.PostedFile.FileName);

//Save files to disk

FileUpload1.SaveAs(Server.MapPath("images1/" + FileName1));

FileUpload1.SaveAs(Server.MapPath("images1/" + FileName2));


FileUpload1.SaveAs(Server.MapPath("images1/" + FileName1));
FileUpload2.SaveAs(Server.MapPath("images1/" + FileName2));



沒有留言:

張貼留言