|
Hi Richard, thanks for replying. I'm using asp.net FileUpload control.
|
|
|
|
|
Well, the documentation for that control says the following:
Quote: The FileContent property gets a Stream object that points to a file to upload. Use this property to access the contents of the file as bytes. For example, you can use the Stream object that is returned by the FileContent property to read the contents of the file as bytes and store them in a byte array. Alternatively, you can use the FileBytes property to retrieve all the bytes in the file.
I would use the FileContent property to get a stream object that reads the bytes of the file.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
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...
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();
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)
{
version = (int) o + 1;
}
}
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
|
|
|
|
|
Thank you so much for replying. Can you please do me one more favor, can you show me how to retrieve the file after it is stored and display it in the browser. None of the tutorials I'm I've looked at worked, I did not get any errors using the code from the tutorials, they just don't do anything. I've been at this for days and I really need to know how to retrieve and display a word doc in the browser as soon as possible. Thanks again for your help.
|
|
|
|
|
The basic answer is: "You can't"
The problem is that this is a Word Document - so there is no native browser support for viewing it, it requires either the appropriate version of Word, or a document converter.
Think about it from a user POV: Browse to your site, press button marked "open CV". In order to view a Word document directly, it must download to his computer (not a problem, easy to do) then open Word and load the file automatically. Here we have the problem:
1) You cannot run an application of any sort directly on his computer because the antivirus will not let you (well, you can in some cases, but it's not a good idea to rely on it, as it requires Windows, IE and ActiveX support, none of which you can rely on).
2) If you can get round (1), you need to force the browser to open the file - which you can't do, again except in specific circumstances, which do not apply to most people. Again, anti virus is going to do it's best to stop you!
3) If you can get round (1) and (2), then you have a problem in that you can't guarantee that Word is loaded on all browser equipped equipment (heck, it isn't even loaded on all PCs!) so you still can't be sure that anyone can read it.
4) If you get round (1), (2) and (3) - and that is a very big "if" - then you have the problem of making sure that the Word file can be read by the version of Word on the Client!
A better approach is to store the DOC / DOCX file and allow downloads, so the user can print / save / share it as necessary, but for viewing either convert it to HTML (which can be shown directly) or an image format (which can again be shown directly). You could (probably, I haven't tried) do both by installing Word on the server, and using Office Interop to load and save the data, or by using a file converter on the server.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
One option that may work for you, there is a microsoft tool that converts documents into .docx format, can you run this on the server that the files are uploaded to. If this is an option and you can get it implemented there are serveral projects out there on the interwebs that show you how to convert a docx file into html, this allows it to be viewed in a browser. I'm not at my work PC at the moment but i have a smaple project on there that allows demonstrates docx files viewed in a web browser.
|
|
|
|
|
I'm afraid you have a race condition in your code. Suppose two files with the same name are being uploaded at the same time. Both could hit the version retrieval code at the same time, which will result in them both getting the same version number. The reason is because there is a delay between the get version and the update part of the code.
|
|
|
|
|
i did write it a long time ago...
Oops! In the field where it is being used it is very unlikely to happen (because the file names are specific to the customer site), but you are absolutely right - I'll fix it in the code next time I release an update - a quick stored proc will fix that!
Thanks for the head up...
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
No problem. I'm glad I can help.
|
|
|
|
|
When I do this, to avoid clashes of file and, I just use a guid as the file name on the disk. Simply store the original file name in your database along with the new name.
|
|
|
|
|
Pete O'Hanlon wrote: I just use a guid as the file name on the disk I read that as quid and thought .
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
That's because you're obsessed with money.
|
|
|
|
|
No, it's because both my eyes and brain are getting more feeble.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Richard MacCutchan wrote: No, it's because both my eyes and brain are getting more feeble
The brain part isn't an issue for me - mine was always feeble. The eyes are rapidly catching up.
|
|
|
|
|
I am using this code to set watermark for textbox but watermark now showing with no error.
here is the code:
private const uint ECM_FIRST = 0x1500;
private const uint EM_SETCUEBANNER = ECM_FIRST + 1;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
and in Form_Load:
SendMessage(txtLoginID.Handle, EM_SETCUEBANNER, 0, "login");
SendMessage(txtPassword.Handle, EM_SETCUEBANNER, 0, "login now");
SendMessage(txtPIN.Handle, EM_SETCUEBANNER, 0, "wow");
SendMessage(txtLoginID.Handle, EM_SETCUEBANNER, 0, "login");
SendMessage(txtPassword.Handle, EM_SETCUEBANNER, 0, "login now");
SendMessage(txtPIN.Handle, EM_SETCUEBANNER, 0, "wow");
|
|
|
|
|
|
jrahma wrote: but watermark now showing with no error
If the watermark is showing without error then what is the problem?
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
he probably meant "not showing"
|
|
|
|
|
My watermark is not showing error AND not showing the watermark either!
|
|
|
|
|
jrahma wrote:
My watermark is not showing error AND not showing
the watermark either!
You question does not say that now does it?
The code that you have posted works perfectly fine on my system.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
Wes Aday wrote: The code that you have posted works perfectly fine on my system.
So you have earned the coveted "Works on My Machine" designation!
Works on My Machine![^]
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Richard Andrew x64 wrote: Works on My Machine
Yeah! Thank you so much! I feel so honored!
I would like to thank the Academy, the directors and producers....
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
The docs for the EM_SETCUEBANNER message say this:
Quote: To use this API, you must provide a manifest specifying Comclt32.dll version 6.0. For more information on manifests, see Enabling Visual Styles.
Enabling Visual Styles[^]
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Basically I am experimenting with Tuple for the first time and I am completely out of my element. I have no idea of how to correct the errors I am getting. Can someone please help.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Testing_Functions
{
class Program
{
public static Tuple<double, double> Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
Double Test_2 = 7;
return Tuple(Test_Val, Test_2);
}
static void Main(string[] args)
{
Double A = 1, B = 2, C = 3;
Tuple Result = Test(A,B,C);
Console.WriteLine("{0}", Result);
}
}
}
|
|
|
|
|
A tuple is a reference object, so you need to create one using the new keyword. Try this :-
public static Tuple<double, double> Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
Double Test_2 = 7;
return new Tuple<double, double>(Test_Val, Test_2);
}
static void Main(string[] args)
{
Double A = 1, B = 2, C = 3;
Tuple<double,double> Result = Test(A, B, C);
Console.WriteLine("{0} {1}", Result.Item1 , Result.Item2 );
Console.ReadLine();
}
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|