Click here to Skip to main content
15,887,355 members
Home / Discussions / C#
   

C#

 
AnswerRe: TypedDataSet synchronisation using compact framework Pin
Pete O'Hanlon20-Aug-12 0:17
mvePete O'Hanlon20-Aug-12 0:17 
GeneralRe: TypedDataSet synchronisation using compact framework Pin
VenkataRamana.Gali20-Aug-12 4:02
VenkataRamana.Gali20-Aug-12 4:02 
QuestionWCF SErvice Authentication Pin
pravin_mun19-Aug-12 20:21
pravin_mun19-Aug-12 20:21 
QuestionGetting File Path Pin
ASPnoob18-Aug-12 17:02
ASPnoob18-Aug-12 17:02 
AnswerRe: etGetting File Path Pin
Richard Andrew x6418-Aug-12 17:17
professionalRichard Andrew x6418-Aug-12 17:17 
GeneralRe: etGetting File Path Pin
ASPnoob18-Aug-12 17:20
ASPnoob18-Aug-12 17:20 
GeneralRe: etGetting File Path Pin
Richard Andrew x6418-Aug-12 17:24
professionalRichard Andrew x6418-Aug-12 17:24 
AnswerRe: Getting File Path Pin
OriginalGriff18-Aug-12 19:42
mveOriginalGriff18-Aug-12 19:42 
File upload controls don'#t give you any path information (because the device originating the file may not support paths, and the path to the original file would give you information about the client machine - which is not allowed for security reasons).

But that's ok, - you don't need it. You need the file name and extension - which all upload controls will give you - and you need the file content as a byte stream - which all upload controls will also give you. You don't need to read it any further to get the data you need, since you can't specific the destination location when any user uploads it anyway!

This is the method I use: it's probably a bit overkill for you since it handles versioning as well, but...

C#
/// <summary>
/// Save an upload into the database.
/// </summary>
/// <param name="fl">Control containing the download.</param>
/// <returns>Status as a string</returns>
private string SaveUpload(FileUpload fl)
    {
    if (fl.HasFile)
        {
        try
            {
            int version = 0;
            string filename = Path.GetFileName(fl.FileName);
            byte[] filedata = fl.FileBytes;
            string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DownloadDatabase"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strCon))
                {
                con.Open();
                // Check version - if the file exists in the DB already, then increase version number.
                using (SqlCommand ver = new SqlCommand("SELECT MAX(version) FROM dlContent WHERE fileName=@FN", con))
                    {
                    ver.Parameters.AddWithValue("@FN", filename);
                    object o = ver.ExecuteScalar();
                    if (o != null && o != System.DBNull.Value)
                        {
                        // Exists already.
                        version = (int) o + 1;
                        }
                    }
                // Stick file into database.
                using (SqlCommand ins = new SqlCommand("INSERT INTO dlContent (iD, fileName, description, dataContent, version, uploadedOn) " +
                                                       "VALUES (@ID, @FN, @DS, @DT, @VS, @UD)", con))
                    {
                    ins.Parameters.AddWithValue("@ID", Guid.NewGuid());
                    ins.Parameters.AddWithValue("@FN", filename);
                    ins.Parameters.AddWithValue("@DS", "");
                    ins.Parameters.AddWithValue("@DT", filedata);
                    ins.Parameters.AddWithValue("@VS", version);
                    ins.Parameters.AddWithValue("@UD", DateTime.Now);
                    ins.ExecuteNonQuery();
                    }
                }
            return string.Format("{0} uploaded, version = {1}", filename, version);
            }
        catch (Exception ex)
            {
            return "The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    return "Please select a file.";
    }

Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

GeneralRe: Getting File Path Pin
ASPnoob18-Aug-12 20:46
ASPnoob18-Aug-12 20:46 
GeneralRe: Getting File Path Pin
OriginalGriff18-Aug-12 21:29
mveOriginalGriff18-Aug-12 21:29 
AnswerRe: Getting File Path Pin
Ed Hill _5_18-Aug-12 23:58
Ed Hill _5_18-Aug-12 23:58 
GeneralRe: Getting File Path PinPopular
Pete O'Hanlon19-Aug-12 1:26
mvePete O'Hanlon19-Aug-12 1:26 
GeneralRe: Getting File Path Pin
OriginalGriff19-Aug-12 1:32
mveOriginalGriff19-Aug-12 1:32 
GeneralRe: Getting File Path Pin
Pete O'Hanlon19-Aug-12 1:52
mvePete O'Hanlon19-Aug-12 1:52 
AnswerRe: Getting File Path Pin
Pete O'Hanlon19-Aug-12 4:51
mvePete O'Hanlon19-Aug-12 4:51 
GeneralRe: Getting File Path ... foolishness Pin
Richard MacCutchan19-Aug-12 5:46
mveRichard MacCutchan19-Aug-12 5:46 
GeneralRe: Getting File Path ... foolishness Pin
Pete O'Hanlon19-Aug-12 5:53
mvePete O'Hanlon19-Aug-12 5:53 
GeneralRe: Getting File Path ... foolishness Pin
Richard MacCutchan19-Aug-12 6:01
mveRichard MacCutchan19-Aug-12 6:01 
GeneralRe: Getting File Path ... foolishness Pin
Pete O'Hanlon20-Aug-12 2:40
mvePete O'Hanlon20-Aug-12 2:40 
Questioncue banner not working Pin
Jassim Rahma17-Aug-12 4:22
Jassim Rahma17-Aug-12 4:22 
AnswerRe: cue banner not working Pin
Ravi Bhavnani17-Aug-12 4:41
professionalRavi Bhavnani17-Aug-12 4:41 
AnswerRe: cue banner not working Pin
Wes Aday17-Aug-12 4:51
professionalWes Aday17-Aug-12 4:51 
GeneralRe: cue banner not working Pin
Shameel17-Aug-12 5:05
professionalShameel17-Aug-12 5:05 
GeneralRe: cue banner not working Pin
Jassim Rahma17-Aug-12 5:06
Jassim Rahma17-Aug-12 5:06 
GeneralRe: cue banner not working Pin
Wes Aday17-Aug-12 5:27
professionalWes Aday17-Aug-12 5:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.