ASP Upload form

PHTech

Senior Member
Joined
Aug 21, 2006
Messages
588
Reaction score
0
Location
Witbank
Hi there...

Can someone please help me with the following code:

<script language="C#" runat="server">
void btnUploadTheFile_Click(object Source, EventArgs evArgs)
{
string strFileNameOnServer = txtServername.Value;
string strBaseLocation = @"c:\Users\Armandus\Documents\local_sites\XCSA_Intranet\pages\sap\repository\";

if ("" == strFileNameOnServer)
{
txtOutput.InnerHtml = "<br><strong>Error - a file name must be specified.</strong>";
return;
}

if (null != uplTheFile.PostedFile)
{
try
{
uplTheFile.PostedFile.SaveAs(strBaseLocation+strFileNameOnServer);
txtOutput.InnerHtml = "<br>File <b>" +
strBaseLocation+strFileNameOnServer+"</b> uploaded successfully.";
}
catch (Exception e)
{
txtOutput.InnerHtml = "<br>Error saving <b>" +
strBaseLocation+strFileNameOnServer+"</b><br>"+ e.ToString();
}
}

}
</script>

<table>
<form enctype="multipart/form-data" runat="server">
<tr>
<td>Select file:</td>
<td><input id="uplTheFile" type="file" runat="server"></td>
</tr>
<tr>
<td>Name in Repository:</td>
<td><input id="txtServername" type="text" runat="server"></td>
</tr>
<tr>
<td colspan="2">
<input type="button" id="btnUploadTheFile" value="Upload" OnServerClick="btnUploadTheFile_Click" runat="server">
</td>
</tr>
</form>
</table>

The field "Name repository" needs to be changed so that the user do not have to specify the file EXTENSION in the repository. It should automatically use the file selected and give it that name in the repository...

Any help please...!
 
...below is the complete ASP file's code.

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SAP Job Aids | Upload files | Library</title>
<link href="../../assets/css/SiteGlobal.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body,td,th {
font-family: Tahoma, Geneva, sans-serif;
}
-->
</style></head>

<body leftmargin="0" topmargin="0">
<div class="HomeHeader"></div>
<div class="DivWrapper">
<div class="MainTable">
<p class="Date">sap job aids library</p>
<div class="SideNav">
<p class="SideNavHeads"><a href="../../index.html" class="SideNavLinks">&raquo; home</a> <a href="repository/" class="SideNavLinks">&raquo; document library</a></p>
<p class="SideNavHeads">&nbsp;</p>
</div>
<p class="Heading1">SAP | Job Aids</p>
<div>
<p>Use the following form to upload assets into the SAP Job Aids Library:</p>
</div>
<div class="MainInfo"><script language="C#" runat="server">
void btnUploadTheFile_Click(object Source, EventArgs evArgs)
{
string strFileNameOnServer = txtServername.Value;
string strBaseLocation = @"c:\Users\Armandus\Documents\local_sites\XCSA_Intranet\pages\sap\repository\";

if ("" == strFileNameOnServer)
{
txtOutput.InnerHtml = "<br><strong>Error - a file name must be specified.</strong>";
return;
}

if (null != uplTheFile.PostedFile)
{
try
{
uplTheFile.PostedFile.SaveAs(strBaseLocation+strFileNameOnServer);
txtOutput.InnerHtml = "<br>File <b>" +
strBaseLocation+strFileNameOnServer+"</b> uploaded successfully.";
}
catch (Exception e)
{
txtOutput.InnerHtml = "<br>Error saving <b>" +
strBaseLocation+strFileNameOnServer+"</b><br>"+ e.ToString();
}
}

}
</script>

<table>
<form enctype="multipart/form-data" runat="server">
<tr>
<td>Select file:</td>
<td><input id="uplTheFile" type="file" runat="server"></td>
</tr>
<tr>
<td>Name in Repository:</td>
<td><input id="txtServername" type="text" runat="server"></td>
</tr>
<tr>
<td colspan="2">
<input type="button" id="btnUploadTheFile" value="Upload" OnServerClick="btnUploadTheFile_Click" runat="server">
</td>
</tr>
</form>
</table>

<span id=txtOutput style="font: 8pt Verdana;" runat="server"/>

<p>Please Note: For each file that you upload, make sure the file extension is also included in the &quot;Name in Repository&quot;. Eg: SAP Document.pdf - It must include an extension like .pdf / .xls / .doc. Below is a list of the most common file extensions that will be used in this form:</p>
<p><span class="Date">.pdf </span>- Portable Document Format | Opens with Adobe Reader</p>
<p><span class="Date">.xls </span>- Microsoft Excel Workbook | Opens with Microsoft Office Excel</p>
<p><span class="Date">.doc </span>- Microsoft Word Document | Opens with Microsoft Office Word</p>
<p><span class="Date">.ppt </span>- Microsoft PowerPoint Document | Opens with Microsoft Office PowerPoint</p>
<p><span class="Date">.txt </span>- Text file | Opens with Notepad (or any text reader)</p></div></div>

<p class="Footer">Copyright &copy; 2009 Xstrata Coal South Africa. All rights reserved | <a href="#" class="FooterLinks">Internet Usage Policy</a> | <a href="#" class="FooterLinks">IT Department</a> | <a href="#" class="FooterLinks">Credits</a></p>
</div>
</body>
</html>
 
So, you don't want to use the name specified in txtServername, but rather the name of the file that was posted back to the server? What would happen if two users upload different files, but with the same name? The previous file would be overwritten then... You should try guard against that by perhaps by adding a GUID prefix to the file name. That would help make the file name unique in the repository on the server. You could try renaming the file as follows:

Code:
System.Guid guid = System.Guid.NewGuid();
string destinationDirectory = @"c:\Users\Armandus\Documents\local_sites\XCSA_Int ranet\pages\sap\repository\";
string fileName = guid.ToString() + "_" + System.IO.Path.GetFileName(uplTheFile.PostedFile.FileName);
uplTheFile.PostedFile.SaveAs(System.IO.Path.Combine(destinationDirectory, fileName));
 
Last edited:
Thanx for that...! How would my code then look after the ammendmends, because I am very stupid with code...

Thanx...
 
Dude, your question is very vague and I don't understand it exactly. Currently the file is being saved as the same file name every time. Do you want to save it as the original file name?
 
Dude, your question is very vague and I don't understand it exactly. Currently the file is being saved as the same file name every time. Do you want to save it as the original file name?

Yes... I want to save it with the original file name and extension as the file selected to upload... How it is now, you specify a file name for the file, but you must specify the file extension as well... Now I just want it to save the file as the original...
 
UPFRONT DISCLAIMER: Code below not tested. No syntax errors found in VS, but didn't build or run.

Thanx for that...! How would my code then look after the ammendmends, because I am very stupid with code...

Thanx...

Try the following:

Code:
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="utf-8" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>SAP Job Aids | Upload files | Library</title>
    <link href="../../assets/css/SiteGlobal.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
<!--
body,td,th {
font-family: Tahoma, Geneva, sans-serif;
}
-->
</style>
</head>
<body leftmargin="0" topmargin="0">
    <div class="HomeHeader">
    </div>
    <div class="DivWrapper">
        <div class="MainTable">
            <p class="Date">
                sap job aids library</p>
            <div class="SideNav">
                <p class="SideNavHeads">
                    <a href="../../index.html" class="SideNavLinks">&raquo; home</a> <a href="repository/"
                        class="SideNavLinks">&raquo; document library</a></p>
                <p class="SideNavHeads">
                    &nbsp;</p>
            </div>
            <p class="Heading1">
                SAP | Job Aids</p>
            <div>
                <p>
                    Use the following form to upload assets into the SAP Job Aids Library:</p>
            </div>
            <div class="MainInfo">

                <script language="C#" runat="server">
                    void btnUploadTheFile_Click(object Source, EventArgs evArgs)
                    {
                        System.Guid guid = System.Guid.NewGuid();
                        string destinationDirectory = @"c:\Users\Armandus\Documents\local_sites\XCSA_Int ranet\pages\sap\repository\";

                        if (uplTheFile.PostedFile == null || uplTheFile.PostedFile.FileName.Length == 0)
                        {
                            txtOutput.InnerHtml = "<br><strong>Error - a file name must be specified.</strong>";
                            return;
                        }

                        if (null != uplTheFile.PostedFile)
                        {
                            try
                            {
                                string fileName = guid.ToString() + "_" + System.IO.Path.GetFileName(uplTheFile.PostedFile.FileName);
                                uplTheFile.PostedFile.SaveAs(System.IO.Path.Combine(destinationDirectory, fileName));
                                txtOutput.InnerHtml = "<br>File <b>" +
                                System.IO.Path.Combine(destinationDirectory, fileName) + "</b> uploaded successfully.";
                            }
                            catch (Exception e)
                            {
                                txtOutput.InnerHtml = "<br>Error saving <b>" +
                                strBaseLocation + strFileNameOnServer + "</b><br>" + e.ToString();
                            }
                        }

                    }
                </script>

                <form id="Form1" enctype="multipart/form-data" runat="server">
                    <table>
                        <tr>
                            <td>
                                Select file:</td>
                            <td>
                                <input id="uplTheFile" type="file" runat="server"></td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <input type="button" id="btnUploadTheFile" value="Upload" onserverclick="btnUploadTheFile_Click"
                                    runat="server">
                            </td>
                        </tr>
                    </table>
                </form>
                <span id="txtOutput" style="font: 8pt Verdana;" runat="server" />
            </div>
        </div>
        <p class="Footer">
            Copyright &copy; 2009 Xstrata Coal South Africa. All rights reserved | <a href="#"
                class="FooterLinks">Internet Usage Policy</a> | <a href="#" class="FooterLinks">IT Department</a>
            | <a href="#" class="FooterLinks">Credits</a></p>
    </div>
</body>
</html>
 
We should be charging for this kind of question.

No offense to the OP, but posting back completed code == work. Helping someone out (like the first few posts) is what we're about, but doing the work for you should be chargeable.
 
We should be charging for this kind of question.

No offense to the OP, but posting back completed code == work. Helping someone out (like the first few posts) is what we're about, but doing the work for you should be chargeable.

As I mentioned in my earlier posts, I am not to good with code, so I posted the complete page's code, and didn't know where to make the ammendmends in my own code.

But I am willing to pay as well if someone actually asked for payment - NO PROBLEM...!
 
But Thanx to FarligOpptreden for actually assisting me without questiones asked. Thanx man..
 
PHTech said:
As I mentioned in my earlier posts, I am not to good with code, so I posted the complete page's code, and didn't know where to make the ammendmends in my own code.

But I am willing to pay as well if someone actually asked for payment - NO PROBLEM...!
:) I was expecting a response.

Nothing personal, dude. You came to the right place, asked the right questions. I just get a little tired of some folks (none in particular) that expect us to provide solutions for them, sometimes at considerable cost (in time, effort) to ourselves. I know that I sometimes go researching to provide the best answer - not everything I write comes from inside my head... :p

So 'scuse the rant. :cool:

PHTech said:
But Thanx to FarligOpptreden for actually assisting me without questiones asked. Thanx man..
Absolutely. :)
 
:) I was expecting a response.

Nothing personal, dude. You came to the right place, asked the right questions. I just get a little tired of some folks (none in particular) that expect us to provide solutions for them, sometimes at considerable cost (in time, effort) to ourselves. I know that I sometimes go researching to provide the best answer - not everything I write comes from inside my head... :p

So 'scuse the rant. :cool:


Absolutely. :)

Cool man...! I understand your main concern here, and I do understand the point... This thread actually is something small for a large project...

But you guys do a good job in helping, and thanx for the responses...
 
Top
Sign up to the MyBroadband newsletter
X