The UploadServiceUrl property in RadUpload for Silverlight 4 seems to always resolve the ~ (tilde) to the root of the web site, not the root of the application, as is the behaviour in .net. Is this a bug?
Example:
I would expect an application running on virtual path "MyVirtualDir" on localhost to resolve the ~ like this:
If i set property UploadServiceUrl="~/MyHandler.ashx", the resulting URL should be:
http://localhost/MyVirtualDir/MyHandler.ashx
However, the follwing seems to be the result:
http://localhost/MyHandler.ashx
King regards,
Anders
23 Answers, 1 is accepted
Please accept my apology for the delayed response.
The scenario you described - using a Virtual Directory is not supported in the RadUpload since the path for the UploadHandler is specified and resolved on the client side. On the client side the virtual directories created on the server side cannot be determined. Therefore in cases that this path is specified using ~(tilde) - UploadServiceUrl="~/SampleUploadHandler.ashx" the tilde is ignored thus returning the same URL returned by UploadServiceUrl="/SampleUploadHandler.ashx".
Please let us know if you need more info.
Greetings,
Tina Stancheva
the Telerik team
I attached an example demonstrating how you can upload images into a database using RadUpload. This example will help you in implementing your scenario. It illustrates how to process uploaded image files on the server-side by overriding the SaveChunkData() method of the RadUploadHandler to process the images and save them into a database. In your case, however, instead of processing the images you can write some custom logic to process the xls files. Also, please keep in mind that in the example a filter is applied to the RadUpload in order to allow uploading only image files so you will need to modify the RadUpload Filter property as well to fit your requirements.
Once the files are processed and saved into a database, you can create a bool property to check whether the changes are reflected in the database, for example like so:
bool Success = (cmd.ExecuteNonQuery()!= 0);
The Success property can then be added into the associatedData dictionary, which will enable you to access it on the client-side to inform the user of the current state of the operation.
I hope this is a good start for you. If you need further info, please let us know.
Regards,
Tina Stancheva
the Telerik team
When I run your example I get uriprefix not recognised error?
You need to set the UploadSaveIntoDataBaseExample.Web project as a StartUp project. Give ti a try and let me know if it works for you.
Regards,
Tina Stancheva
the Telerik team
I do appreciate this might not be the correct forum now so if not I thank you for the help so far it has been great.
in order to implement your scenario, you can modify the SaveChunkData() event to upload the data from the uploded excel files to a database following the approach suggested here.
I hope this information will get you started. Let us know if we can further assist you.
Sincerely yours,
Tina Stancheva
the Telerik team
We do not have that luxury - in the past with asp.net I could get the bytes from the upload control and save that to a database.
With the Silverlight control is seems that I first must use base.SaveChunkData(filePath.....
Am I missing something here?
Thanks,
Steve
Indeed the RadUpload for Silverligth is designed to upload files on the server in a specified storage. So in order to workaround this, you will need to override the RadUploadHandler ProcessStream() method to control the upload process and save the files only to the database:
public
override
void
ProcessStream()
{
this
.AddReturnFileParam(RadUploadConstants.ParamNameFinalFileRequest,
this
.IsFinalFileRequest());
if
(
this
.IsFinalFileRequest())
{
HttpPostedFile fileSource =
this
.Request.Files[0];
string
UserID =
this
.GetQueryParameter(
"UserID"
);
string
connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[
"FilesDataBaseConnectionString"
].ConnectionString;
//processing the files
byte
[] array =
new
byte
[fileSource.ContentLength];
fileSource.InputStream.Read(array,0,fileSource.ContentLength);
using
(SqlConnection conn =
new
SqlConnection(connectionString))
{
string
cmdText =
"INSERT INTO FileData(FileData, FileName, UserID) VALUES(@FileData, @FileName, @UserID) SET @Identity = SCOPE_IDENTITY()"
;
SqlCommand cmd =
new
SqlCommand(cmdText, conn);
SqlParameter identityParam =
new
SqlParameter(
"@Identity"
, SqlDbType.Int, 0,
"FileID"
);
identityParam.Direction = ParameterDirection.Output;
cmd.Parameters.AddWithValue(
"FileData"
, array);
cmd.Parameters.AddWithValue(
"FileName"
,
this
.GetFileName());
cmd.Parameters.AddWithValue(
"UserID"
, UserID);
cmd.Parameters.Add(identityParam);
conn.Open();
//checking whether the operation is successfull
Success = (cmd.ExecuteNonQuery() != 0);
FileID = (
int
)identityParam.Value;
}
}
this
.SendFeedback(Success,
this
.GetFileName(),
this
.GetFileName());
}
The code snippet above can help you get started. However, in cases when the uploaded file's size it bigger than the buffer's size, the above ProcessStream() implementation has to be customized to keep track of all file chunks sent to the UploadHandler and when the last one is received, to put the file together and save it into the database.
Best wishes,
Tina Stancheva
the Telerik team
thanks
The Tine's example in the previous post shows how you can override the ProcessStream method, without saving the file's chunks to the file system. Isn't that helpful or you have some other requirement?
Best wishes,
Alex Fidanov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Thanks
You can assume that the bytes are coming in a consecutive manner - from the start to the end. The first chunk is the start of the file and the last one is the end. Therefore, you can store them in an array or any collection by adding each chunk of bytes at the end of the array. When the final chunk arrives, the file should be complete.
Best wishes,
Alex Fidanov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
"Therefore, you can store them in an array or any collection by adding each chunk of bytes at the end of the array"
But when I try to define a variable in the upload handler, the variable gets re-initialized every time a new chunk gets saved. How do I define a variable so that it persists across multiple chunks?
I will wrap up our discussion here as well so that the community can keep track of it as well.
Basically in order to save large files directly in a database for example, you can override the SaveChunkData() method as demonstrated in the attached solution.
However the approach demonstrated in the sample throws "Size not big enough" exception when uploading very large files. Therefore depending on everyone's scenario, the RadUploadHandler implementation may have to be extended - instead of uploading in the database the entire file stream, when it is too large, you can upload it in a couple of chunks.
All the best,Tina Stancheva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
I am not sure why you can't debug the project on your side, but if you can debug a new Silverlight solution on your machine, you can just recreate the project. For that purpose, you'll have to create a new Silverlight solution and copy the code of the following files:
- MainPage.xaml and MainPage.xaml.cs
- SampleUploadHandler.ashx and SampleUploadHandler.ashx.cs
- StreamFile.ashx and StreamFile.ashx.cs
Kind regards,
Tina Stancheva
the Telerik team
Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.
I modified the solution to traverse the files table to check if a file with the same name already exists in it. Then I make sure to insert the uploaded file with a unique new name in case its name is already used in the table.
The changes I implemented are mainly in the SampleUploadHandler implementation. Please note that I modified the SaveChunkData() method implementation to create a new query to the sql DataBase and find if a file name already exists. In the SampleUploadHandler, you can use the GetFileName() method to get the name of the file that is being currently uploaded. Still I created a new string property to keep the current name - the one returned by the GetFileName() method or a new unique name if the file has already been saved in the database.
I hope this solution will get you started. Let us know if we can further assist you.
Kind regards,
Tina Stancheva
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
,not like assign the random letter or number in server side,in that u change the code like if name is exist u call a GetFileName() method, instead u call a GetFileName() method,u popup window to rename.pls reply asap thanx
The RadUploadHandler returns a response to the client side of the RadUpload control only after the upload session is over - successfully or unsuccessfully. In both case, the upload operation will be finished by the time you get to display a notification window or a popup to the client using Silverlight controls.
This is why I'd recommend validating each file - using the RadUploadItem.ValidateEvent you can call your WCF service to check if the name of the file has already been entered in the database. Here, you'll also be able to provide a popup or a RadWindow prompting the user to enter a new name. As this logic is implemented in the Silverlight project, you can use any other Silverlight controls to build your UI and logic.
I hope this information will help you.
All the best,
Tina Stancheva
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
just came around this problem. May some others might find this helpful:
I solved it quite simple and straight. Placed the Upload.ashx in the ClientBin Folder. (Or wherever, adjust replacement below)
defined HandlerName in Markup:
<
telerik:RadUpload
UploadServiceUrl
=
"Upload.ashx"
>
and simple updated the value in constructor with url containing virtual folder:
public
MainPage()
{
InitializeComponent();
RadUpload1.UploadServiceUrl = App.Current.Host.Source.AbsolutePath.Replace(
"Silverlight.xap"
, RadUpload1.UploadServiceUrl);
}
done. cheers otto.