I am trying to use GUID to allow users to upload files with same names, but the GUIDs attach to filenames & filepaths stored in database are different from the GUIDs attached to the files stored on server. It means it is giving two different GUIDs to the same file. So when I am trying to download the file from server its giving me an FileNotFound exception as the command arguement takes the filename from database and therefore when it goes to server its not able to find the file as the GUID of the same file stored on the server is different. Please if anybody can give me some advise that would be appreciable
My Database schema:
<pre lang="sql">Table: Sales
Columns: ReceiptFileName(Stores Name without GUID)
Coulmn:ReceiptFilePath(Stores file path with GUID)
Column: filename(Stores filename with GUID)</pre>
Aspx code:
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" CommandArgument='<%# Bind("filename") %>' Text='<%# Bind("ReceiptFileName") %>' ></asp:LinkButton>
</ItemTemplate>
Code for upload:
if (FileUpload1.HasFile)
{
string filename = FileUpload1.PostedFile.FileName;
{
filename = filename + Guid.NewGuid();
}
int filesize = FileUpload1.PostedFile.ContentLength;
if (filesize > (20 * 1024))
{
Label1.Text = "Please upload a zip or a pdf file";
}
string fileextention = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileextention.ToLower() != ".zip" && fileextention.ToLower() != ".pdf")
{
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = "Please upload a zip or a pdf file";
}
else
{
string ReceiptFileName = Path.GetFileName(filename);
FileUpload1.SaveAs(Server.MapPath("~/Reimbursement/Reciepts/" + ReceiptFileName));
}
}
Method for adding filename and receiptname
public static int AddSale(SaleID, string ReceiptFileName,string ReceiptFilePath, string filename)
{
int sale;
SqlCommand addSales = new SqlCommand("addSale", con);
addSale.CommandType = CommandType.StoredProcedure;
addSale.Parameters.AddWithValue("@ReceiptFileName", ReceiptFileName);
addSale.Parameters.AddWithValue("@filename", filename);
addSale.Parameters.AddWithValue("@ReceiptFilePath", ReceiptFilePath);
try
{
con.Open();
added = addSale.ExecuteNonQuery();
}
finally
{
con.Close();
}
Code for download:
protected void gridExpenditures_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("content-disposition", "FileName=" + e.CommandArgument);
Response.TransmitFile(Server.MapPath("~/Reimbursement/Reciepts/") + e.CommandArgument);
Response.End();
}
}