Reply
Old 04-13-2009, 09:02 AM file upload
Average Talker

Posts: 24
Name: Adam Kischinovsky
Trades: 0
Hi I'm trying to make a file upload function for pictures.
So can anyone please help me to make a simple file upload function in c#?

I've tried to make one, but it won't seem to work, so I've decided to start over, just with a simple file upload funktion.

Hope someone can help?
Kischi is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 04-13-2009, 10:15 PM Re: file upload
Skilled Talker

Posts: 69
Trades: 0
use the asp.net fileupload control and a button for starting the upload:

<asp:FileUpload ID="fileToBeUploaded" runat="server" />
<asp:Button ID="btnUpload" Text="Upload" runat="server" />

in the button click event put the following:

string fn = System.IO.Path.GetFileName(fileToBeUploaded.Posted File.FileName);

fileToBeUploaded.SaveAs(Server.MapPath(folderLocat ion) + fn);

Hope this helps.
stbuchok is offline
Reply With Quote
View Public Profile
 
Old 04-13-2009, 10:18 PM Re: file upload
Skilled Talker

Posts: 69
Trades: 0
sorry forgot to mention, by default there is a 4mb limit. If you want to allow for greater file sizes you will need to increase the MaxRequestLength in the web.config file. Just google it and there will be instructions.
stbuchok is offline
Reply With Quote
View Public Profile
 
Old 04-14-2009, 05:10 PM Re: file upload
Average Talker

Posts: 24
Name: Adam Kischinovsky
Trades: 0
Ok thx, that works fine if the user uploads a file,
But I also wan't it to work if the user dousn't upload a file.

Then the text that the person writes should just be put into the database.

My code looks like this:

aspx page:

Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/admin/MasterPage.master" AutoEventWireup="true" CodeFile="skriv_beskrivelse_tekniker.aspx.cs" Inherits="admin_skriv_beskrivelse_tekniker" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

Titel:<br /><textarea id="titel" cols="35" runat="server" rows="7"></textarea><br /><br />

indsæt billede:<br /><asp:FileUpload ID="fu" runat="server"/><br />

<asp:Button ID="opret" runat="server" Text="opret beskrivelse" OnClick="opret_Click"/>

<asp:Label ID="lblOutput" runat="server"></asp:Label><br />

</asp:Content>
cs page:

Code:
protected void opret_Click(object sender, EventArgs e)
    {

        SqlCommand objcmd = new SqlCommand();
        objcmd.CommandType = CommandType.Text;
        objcmd.Connection = objconn;
        objcmd.CommandText = "UPDATE TEKNIKKERE SET beskrivelse = @beskrivelse, img_url = @img_url WHERE id = @id";
        objcmd.Parameters.AddWithValue("@beskrivelse", (titel.Value.Replace(Environment.NewLine, "<br />")));
        objcmd.Parameters.AddWithValue("@img_url", fu.FileName);
        objcmd.Parameters.AddWithValue("id", Request.QueryString["id"]);
         


        objconn.Open();

        objcmd.ExecuteNonQuery();

        objconn.Close();


        string fn = System.IO.Path.GetFileName(fu.PostedFile.FileName);

        fu.SaveAs(Server.MapPath("../admin/images/") + fn);




            
            }

            


            }
now if the user only types text in the textbox an error page comes up with this error:

Could not find a part of the path 'D:\hshome\kischi2\radio.web.surftown.dk\admin\ima ges\'.


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\hshome\kischi2\radio.web.surftown.dk\admin\ima ges\'.

Source Error:

Line 47: string fn = System.IO.Path.GetFileName(fu.PostedFile.FileName) ;
Line 48:
Line 49: fu.SaveAs(Server.MapPath("../admin/images/") + fn);
Line 50:
Line 51:

I assume it is because it is looking for a file to put into the folder "images" but there is no file.
But I am not sure.

I hope you understand what I mean and can help me?

Thanks
Kischi is offline
Reply With Quote
View Public Profile
 
Old 04-14-2009, 09:04 PM Re: file upload
Skilled Talker

Posts: 69
Trades: 0
do a check to make sure fn is not an empty string. If it's not empty then you can save the file.
stbuchok is offline
Reply With Quote
View Public Profile
 
Old 04-15-2009, 02:34 AM Re: file upload
Average Talker

Posts: 24
Name: Adam Kischinovsky
Trades: 0
Ok, but how do I do that, I don't really know how to check if the string fn is empty.

I thought maybe could do something like this, please correct me if im wrong:

Code:
if (fu.HasFile)
        {

            string fn = System.IO.Path.GetFileName(fu.PostedFile.FileName);

            fu.SaveAs(Server.MapPath("../admin/images/") + fn);

        }
        else
        {
                //don't, but I don't know how I can write it here in c# code
        }
Thx
Kischi is offline
Reply With Quote
View Public Profile
 
Old 04-15-2009, 04:04 PM Re: file upload
Skilled Talker

Posts: 69
Trades: 0
Something like this should do.

string fn = System.IO.Path.GetFileName(fu.PostedFile.FileName) ;

if (fn != string.Empty)
{
fu.SaveAs(Server.MapPath("../admin/images/") + fn);
}
stbuchok is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to file upload
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.12679 seconds with 13 queries