is it an issue of moving/renaming a folder of asp.net? - asp.net

I would like to rename a folder with asp.net:
string oldFolderTitlePath = ServerPhyscialPath + oldFolderTitle + "/";
string newFolderTiltePath = ServerPhyscialPath + newFolderTille+ "/";
DirectoryInfo diPath = new DirectoryInfo(oldFolderTitlePath);
if(diPath.Exists)
{
///Now move(Rename) folder on the server
Directory.Move(oldFolderTitlePath, newFolderTiltePath);
}
I wonder that if the old folder contains number of files and the size is more than 1GB. Will it take a lot of time to rename a folder on asp.net?
Thanks in advance.

Generally, no it should not take a lot of time. You're basically changing the name of the directory not actually moving its contents on the disk.
That said, I'd be very careful with doing what you're doing. I'm always wary of IO operations from ASP.NET -- the reason: Many users could potentially be executing this code at the same time. That could lead to all sorts of problems. You need to make sure this operation is thread safe (perhaps by locking a static variable).
http://msdn.microsoft.com/en-us/library/c5kehkcz%28v=vs.71%29.aspx
http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx

Related

Convert ASP.NET Web Form App with Image Uploads to Azure

So I'm working on moving a web app to Azure and the one thing that is tripping me up is how to deal with image uploads. Currently, I'm handling uploads with ImageResizer and saving them to a directory:
Protected Function DoHeaderUpload() As Guid?
If HeaderUploadControl.HasFile Then
If HeaderUploadControl.PostedFile.ContentType.StartsWith("image/") Then
If HeaderUploadControl.PostedFile.ContentLength < 3145728 Then ' 3MB max size
Dim myFileGuid = System.Guid.NewGuid()
Dim i As ImageResizer.ImageJob = New ImageResizer.ImageJob(HeaderUploadControl.PostedFile,
"~/Images/Headers/" & myFileGuid.ToString() & ".jpg",
New ImageResizer.Instructions("height=100;format=jpg;mode=crop"))
i.Build()
Return myFileGuid
Else
HeaderUploadError.Text = "<br>File exceeds 3MB maximum size"
HeaderUploadError.IsValid = False
End If
Else
HeaderUploadError.Text = "<br>Only image uploads are supported"
HeaderUploadError.IsValid = False
End If
End If
End Function
I found a code excerpt that will populate a blob from the memory stream output from ImageResizer (link).
However, I'm a bit unclear on how to embed/reference these images in my site. Currently I'm just doing this (for example):
HeaderImage.Src = String.Format("/Images/Headers/{0}.jpg", .HeaderImage.ToString())
I guess I could just include the full URL prefix to the blob, but that seems like a bad idea (and would break things when running locally for testing).
Could I use a virtual path that points to an Azure Files share instead of Azure Blob?
I saw AzureReader2 for ImageResizer which is tempting, but it seems super expensive. It seems like that could be a pretty easy solution as well, since it looks like it auto-redirects references with a certain prefix to the Azure Blob location.
I guess the question is: what is the easiest way to do this? I've kind of been going in circles with the various options.
Thanks!
I ended up using the solution where I prefix the appropriate URL to get to the blob storage. To make it work locally and remotely, I added a transform:
<appSettings>
<add key="BlobURLPrefix" value="http://myapp.blob.core.windows.net"
xdt:Locator="Match(key)"
xdt:Transform="SetAttributes(value)"/>
</appSettings>
Then when I need to reference an image location, I can do something like this:
FullUrl = ConfigurationManager.AppSettings("BlobURLPrefix") + ImagePath
Not entirely elegant, but it suits my purposes reasonably well and is pretty easy to implement.

ASP.NET creating resources at runtime

I'm developing an ASP.NET webapp that has a multilanguage feature allowing the webmaster to create new languages at runtime.
The approach that I was thinking is the following:
The user selects one available (not created) language.
When the user confirms, the application automatically copies a set of existing resources, replacing the filename with the new culture. For example: default.aspx.en-us.resx to default.aspx.es-ar.resx.
The user edits the recently created resources.
Currently I'm having troubles with step number 2. I've achieved to copy the resources, but then these new resources are ignored. I think that this happens because the new resources are not included in the running assembly, and therefore are being ignored.
When I test the following code in my local project, I would have to manually add the new resources to the solution and then recompile to make it work.
Does anyone know how to make this work?
This is the code of the mentioned copy.
string _dir = path_ + "App_LocalResources\\\\";
DirectoryInfo _dirInfo = new DirectoryInfo(_dir);
foreach (FileInfo _file in _dirInfo.GetFiles("*en-us.resx")) {
_file.CopyTo(_dir + _file.Name.Replace("en-us", idioma_.Cultura));
}
string _dir2 = path_ + "App_GlobalResources\\\\";
_dirInfo = new DirectoryInfo(_dir2);
foreach (FileInfo _file in _dirInfo.GetFiles("*en-us.resx")) {
_file.CopyTo(_dir2 + _file.Name.Replace("en-us", idioma_.Cultura));
}
Thank you very much.
Creating or editing Resource files is not possible the same way as reading data.
In order to create or edit a resource file, you should do it the same way you create or edit XML files because resource files have with a specific structured XML elements.
Maybe this article will help you...

Downloading multiple files from one Gridview row asp.net C#

I have an application that downloads files from within a gridview. When a single file is attached to a row I can easily download the files but when I try to have multiple files within a row my application can no longer download. Could anybody possibly suggest a way I could accomplish this?
Thanks in advance
When user click and as to download a file you can give this javacript command
window.location = "FileForDownload.jpg";
If you won to give the user the ability to download more than one files is more complicate.
Firs you must need to know that the browser will alert that action and stop it until user accept the multiple downloads.
Now you could easy say that you can send many files at window.location but if this happens with out synchronization then the one file can stop the other. The synchronization here can be done with this trick of cookies and give the command the second download only after the first have been start (or end).
One more solution is to call the window.open("FileForDownload.jpg"); many times (with a timer delay of course) that is not need synchronization.
This is a general idea in javascript, but need some improvement to open the files with a delay.
function DownloadAllFiles()
{
for (var i=0; i< arguments.length; i++){
window.open( "http://www.domain.com/downloadpath/" + arguments[i]);)
}
}
// example of call
DownloadAllFiles("File1.zip","File2.zip","File3.zip");

Exception in deleting image uploaded to the server in ASP.Net

In asp.net, we have uploaded a .jpeg file and saved this as bitmap image using following code
HttpPostedFile uploadFile;
System.IO.Stream stream = uploadFile.InputStream;
using (System.Drawing.Image imgSource = System.Drawing.Bitmap.FromStream(stream))
{
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
using (System.Drawing.Image imgThumbnail = imgSource.GetThumbnailImage(imgSource.Width, imgSource.Height, myCallback, IntPtr.Zero))
{
imgThumbnail.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
imgThumbnail.Dispose();
}
imgSource.Dispose();
}
stream.Close();
stream.Flush();
stream.Dispose();
After upload, if we perfrom delete operation it throws error.
We are following code to do so;
if (File.Exists(filePath))
{
File.Delete(filePath);
}
The exception says:The process cannot access the file 'abc.jpg' because it is being used by another process.
Does anyone know, why this is happening ?
Thanks in advance.
The garbage collector doesn't release it's resources to your file immediately.
You can try and run GC.Collect() to "force" the garbage collector to do it's thing. Do this after you have Disposed your file and before you try and delete it.
Edit: I you have some anti virus software on your machine it's likely that it is what is using your file. Same if you have an Explorer window open to the folder where the image is created etc.
How about trying first flushing and then closing the stream?
Also:
The using construct takes care of calling Dispose() for you. You don't need to do it explicitly, and in fact, doing so might break improperly implemented IDisposables.
Are you absolutely sure that filename is different each time? If not, concurrent requests may overwrite each others' files, so the exception you're getting may be caused by one thread trying to delete the file while another is overwriting it.

Preventing Flex application caching in browser (multiple modules)

I have a Flex application with multiple modules.
When I redeploy the application I was finding that modules (which are deployed as separate swf files) were being cached in the browser and the new versions weren't being loaded.
So i tried the age old trick of adding ?version=xxx to all the modules when they are loaded. The value xxx is a global parameter which is actually stored in the host html page:
var moduleSection:ModuleLoaderSection;
moduleSection = new ModuleLoaderSection();
moduleSection.visible = false;
moduleSection.moduleName = moduleName + "?version=" + MySite.masterVersion;
In addition I needed to add ?version=xxx to the main .swf that was being loaded. Since this is done by HTML I had to do this by modifying my AC_OETags.js file as below :
function AC_FL_RunContent(){
var ret =
AC_GetArgs
( arguments, ".swf?mv=" + getMasterVersion(), "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
, "application/x-shockwave-flash"
);
AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}
This is all fine and works great. I just have a hard time believing that Adobe doesn't already have a way to handle this. Given that Flex is being targeted to design modular applications for business I find it especially surprising.
What do other people do? I need to make sure my application reloads correctly even if someone has once per session selected for their 'browser cache checking policy'.
I had a similar problem, and ended up putting the SWF files in a sub-directory named as the build number. This meant that the URL to the SWF files pointed to a different location each time.
Ideally this should be catered for by the platform, but no joy there. But this works perfectly for us, and integrates very easily into our automated builds with Hudson - no complaints so far.
Flex says:
http://www.adobe.com/livedocs/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001388.html
What I have done is checksum the SWF file and then add that to its url. Stays the same until the file is rebuilt/redeployed. Handled automagically by a few lines of server-side PHP script
here is sample.
function AC_FL_RunContent(){
var ret = AC_GetArgs(arguments, ".swf?ts=" + getTS(), "movie",
"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000",
"application/x-shockwave-flash");
AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}
function getTS() {
var ts = new Date().getTime();
return ts;
}
AC_OETags.js is file and it exists html-template several places.
but as my posting said, I am facing another type of problem.
The caching is not done by Flash Player but by the browser, so it's out of Adobe's control. I think you have found a workable solution. If I want to avoid caching I usually append a random number on the URL.

Resources