Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
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:
XML
<ItemTemplate>
     <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" CommandArgument='<%# Bind("filename") %>' Text='<%# Bind("ReceiptFileName") %>' ></asp:LinkButton>
</ItemTemplate>



Code for upload:
C#
if (FileUpload1.HasFile)
{
    //check file Extension & Size
    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(FileUpload1.PostedFile.FileName);
        string ReceiptFileName = Path.GetFileName(filename);

        //save file to disk
        FileUpload1.SaveAs(Server.MapPath("~/Reimbursement/Reciepts/" + ReceiptFileName));
    }
}


Method for adding filename and receiptname
C#
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:
C#
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();
    }
}
Posted
Updated 19-Jun-14 7:42am
Comments
ZurdoDev 19-Jun-14 13:42pm    
Why does the GUID change?
ray thomsan 19-Jun-14 13:43pm    
Thats what I am trying to figure out
ZurdoDev 19-Jun-14 13:55pm    
Just debug it.
Jafarinejadvazifehkhorani 19-Jun-14 15:00pm    
it seems that GUID is generating two times, one before saving the file and the other one just before inserting into the table. if you want us to help more, please add more details about the code
Prasad Avunoori 20-Jun-14 2:31am    
"ReceiptFileName" in your ItemTemplate should contain the full path of the file, does it contain?

1 solution

Just a suggestion.
If you can add a column in your database, why not add a FileID with the type VARCHAR or GUID and make FileID the primary key.
It is not a direct solution to your actual problem, but more of a different approach.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900